简体   繁体   English

我如何在Java中将外部数据放入数组中

[英]How I put external data in array at Java

In my university I have to do a project that includes a part where I have to right my own coordinate system with Java and it has to be dynamic, and an applet. 在我的大学里,我必须做一个项目,其中包括一个部分,其中我必须使用Java纠正我自己的坐标系,并且它必须是动态的,以及一个applet。

It is mostly already done but I don't know how I can put external data into my arrays. 它大部分已经完成了,但是我不知道如何将外部数据放入数组中。 I don't know how many parameters I get (it could be 10 but it also could be 10000). 我不知道我得到了多少参数(可能是10,但也可能是10000)。

So how do I tell the array how much space it needs and how do I put the parameters into it? 那么如何告诉数组需要多少空间以及如何将参数放入数组呢?

It will be always x and y coordinates, so I have 2 arrays. 它将始终是x和y坐标,所以我有2个数组。

This is the code I already tried. 这是我已经尝试过的代码。 I have hardcoded parameters for the array just to see if it works. 我为数组设置了硬编码参数,只是看它是否有效。

underneath the java code i post te html code.In that are the parameter which I want add to my arrays. 我在Java代码下面发布了html代码,这是我想添加到数组中的参数。

import java.awt.*;
import java.applet.Applet;
import java.lang.Math;

public class graph extends java.applet.Applet {
    //Array x-Werte
    private double[] WerteX= {0.0, 5.5, 6.5, 7.0, 16.6, 50.6};

    //Array y-Werte
    private double[] WerteY= {0.0 , 100.0, 320.0, 550.0, 700.0, 0.3};

    public void init() {
        // Hintergrundfarbe schwarz
        setBackground (Color.black);
    }

    public void paint (Graphics g) {
        float x, y;
        int xnorm, ynorm, i;

        // initialisierung der min und max X/Y werte damit sich das System anpasst
        double minX= WerteX [0];
        double maxX= WerteX [0];
        double minY= WerteY [0];
        double maxY= WerteY [0];

        // zum Kleinsten bzw. größten Wert im Array raussuchen (Prinzip Bubbelsotrt (ungefähr))
        for ( i=1; i<WerteX.length; i++) {

            if (WerteX[i]<minX){
                minX=WerteX[i];
            }
            if (WerteX[i]>maxX){
                maxX=WerteX[i];
            }
            if (WerteY[i]<minY){
                minY=WerteY[i];
            }
            if (WerteY[i]>maxY){
                maxY=WerteY[i];
            }
        }

        // Vordergrundfarbe Rot fuer die Achsen
        g.setColor (Color.red);

        // X-Achse zeichnen (schon skaliert auf 500%)
        g.drawLine (0, 650, 650, 650);

        // Y-Achse zeichnen (schon skaliert auf 500%)
        g.drawLine (100, 0, 100, 650);

        // Skaleneinteilung fuer die X-Achse zeichnen
        for (i = 100; i <= 750; i += 150){
            g.drawLine (i, 0, i, 650);
        }

        // Skaleneinteilung fuer die Y-Achse zeichnen
        for (i = 0; i <= 650; i += 50){
            g.drawLine (100, i, 750, i);
        }

        double Irgendwasdx=maxX-minX;               // achsen bezeichnung 
        double Irgendwasdy=maxY-minY;

        maxX+=Irgendwasdx/12;

        maxY+=Irgendwasdy/12;

        g.setColor(Color.red);              // farbe des Systems und schrift
        g.setFont(new Font("Arial", Font.BOLD, 20));

        // Koordinatenbezeichnung x-Achse       

        i=0;

        for ( double plotty= minX; plotty<=maxX; plotty+=(maxX-minX)/(13.0/3.0)){

            g.drawString(String.valueOf(Math.round(plotty)), 100+i*150, 670);       //runden
            i++;
        }

        //Koordinatenbezeichnung y-Achse
        i=0;

        for ( double plotty= minY; plotty<=maxY; plotty+=(maxY-minY)/13){

            g.drawString(String.valueOf(Math.round(plotty)), 80 ,650-50*i); //runden
            i++;
        }

        int size = WerteX.length; 

        g.setColor (Color.white);

        for (i=0; i<size; i++)  {               //Punkte
            g.fillOval ((int)(WerteX[i]*650/(maxX-minX)-650*minX/(maxX-minX))+100-3, 650-(int)(WerteY[i]*650/(maxY-minY)-650*minY/(maxX-minY))-3, 6, 6);
        }                       
        //Punkte verbinden
        for (i=0; i<size-1; i++) {
            g.drawLine ((int)(WerteX[i]*650/(maxX-minX)-650*minX/(maxX-minX))+100, 650-(int)(WerteY[i]*650/(maxY-minY)-650*minY/(maxX-minY)),(int)(WerteX[i+1]*650/(maxX-minX)-650*minX/(maxX-minX))+100, 650-(int)(WerteY[i+1]*650/(maxY-minY)-650*minY/(maxX-minY)));
        }

    }

}

<html>
<head>
</head>
<body>

<Applet code="graph.class" width="700" height="700">

<param name= "P" value=5>

<param name="x0" value="0.60">
<param name="x1" value="1.20">
<param name="x2" value="2.50">
<param name="x3" value="3.40">
<param name="x4" value="3.80">

<param name = "N" value =5>

<param name="y0" value="0.00">
<param name="y1" value="0.12">
<param name="y2" value="0.25">
<param name="y3" value="0.37">
<param name="y4" value="0.50">


</Applet>

</body>
</html>

Constructing an array for doubles of size X 为大小为X的双精度构造数组

double[] NameOfYourArray = new double[x];

Constructing an array / list - type that can take infinite objects / doubles (in this case) 构造一个数组/列表-可以接受无限个对象/双打的类型(在这种情况下)

ArrayList<Double> nameOfYourArray = new ArrayList<Double>();
nameOfYourArray.add(NEW_ELEMENT); // to add an element of type Double.
nameOfYourArray.get(INDEX); // to get the element at position INDEX

Coordinates 座标

Java is an object oriented language, meaning that you can represent anything with an object. Java是一种面向对象的语言,这意味着您可以用对象表示任何东西。 Objects are made by defining classes. 通过定义类来创建对象。 Just like Double, ArrayList, String, Integer and such are classes. 就像Double,ArrayList,String,Integer等都是类。 We can make our own class Coordinate that holds an x and y. 我们可以使自己的类Coordinate包含x和y。

public class Coordinate {
    private double x; private double y;

    public Coordinate(double x, double y){
        this.x = x; this.y = y;
    }

    public double getX(){ return x; }
    public double getY(){ return y; }
}

To make the Coordinate system work, you'll need to change the type of your ArrayList to Coordinate though, instead of Double. 为了使Coordinate系统正常工作,您需要将ArrayList的类型更改为Coordinate,而不是Double。

Also, define Coordinate in a new file with name Coordinate.java . 另外,在名为Coordinate.java的新文件中定义Coordinate。 It should live in the same directory as your current file. 它应该与当前文件位于同一目录中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM