将整数扫描成2D arraylist?

时间:2013-11-30 16:04:49

标签: java arraylist

我尝试了不同的变化,但我不知道如何让它工作。我试图让用户输入基于用户选择的数字循环的坐标,但扫描仪似乎不能很好地与2d阵列一起工作。

编辑:添加了其余的代码以阐明我想要实现的内容,我需要能够从数组中删除行并让其余部分向上移动并对它们使用数学运算。

import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;

public class ConvexHull {

public static void main(String[] args){

    int grid[][] = new int [10][10];
    int points = 0;
    boolean upperhull = false;
    boolean lowerhull = false;

    Scanner intScanner = new Scanner(System.in);

    System.out.println( "Enter number of coordinates to evaluate: " );
    points = intScanner.nextInt();

    ArrayList coords[][] = new ArrayList[points][2];

    for(int i=0; i<points; i++){
        System.out.println( "Enter the x-coordinate for point " + ( i + 1 ) );
        coords.add([i][0], intScanner.next());

        System.out.println( "Enter the y-coordinate for point " + ( i + 1 ) );
        coords[i][1] = intScanner.nextInt();
    }
        for (int i=0; i <= coords.length; i++)
         for (int x=0; x < coords.length-1; x++)
              if (coords[x][0] > coords[x+1][0])
                {
                    int temp[] = coords[x];
                    coords[x] = coords[x+1];
                    coords[x+1] = temp;
                }

    for(int j=0; j<points; j++){    
    System.out.println("Co-ordinates are " + coords[j][0] + "," + coords [j][1]);   
    }

    for(int k=0; k<points-2; k=k-1){


    int ax = coords[k][0];
    int ay = coords[k][1];
    k++;
    int bx = coords[k][0];
    int by = coords[k][1];  
    k++;
    int cx = coords[k][0];
    int cy = coords[k][1];


    int turn = (bx - ax)*(cy-ay)-(by-ay)*(cx-ax);

        if (k==2){
            if (turn > 0){
                lowerhull = true;
                System.out.println("Computing the lower hull");
            }
            else if (turn < 0){
                upperhull = true;
                System.out.println("Computing the upper hull");
            }
            else if (turn == 0){
                System.out.println("Its a straight line");
            }
                }
        else if(lowerhull = true && turn < 0){
            coords[k-1][0] = 0;
            coords[k-1][1] = 0; 
        }
        else if(upperhull = true && turn > 0){
            coords[k-1][0] = 0;
            coords[k-1][1] = 0; 
        }

    }

    for(int j=0; j<points; j++){    
    if(coords[j][0] != 0 && coords [j][1] !=0)
    System.out.println("The convex hull points are " + coords[j][0] + "," + coords [j][1]);
}
}

}

3 个答案:

答案 0 :(得分:0)

正如您所声明的那样,coords是“ArrayLists数组数组”类型的变量。那是为什么?

您需要将coords声明为

int[][] coords = new int[points][2];

然后只需指定如下值:

coords[i][0] = intScanner.next();

好吧,我认为这是Java语言。如果没有,那么我的答案可能需要改进。

答案 1 :(得分:0)

现在我很可能错了,但我认为你不了解2D阵列是如何工作的。

网格中的二维数组字,有点列和行。使用ArryList实现它的方式只会让你更难。

您应该将值存储在字符串的ArrayList中。

ArrayList<String> co = new ArrayList<String>();

并使用用户的两个输入并将其添加到ArrayList。

String c = "";
for(int i=0; i<points; i++){
c = "";
System.out.println( "Enter the x-coordinate for point " + ( i + 1 ) );
c = c + intScanner.nextInt();

System.out.println( "Enter the y-coordinate for point " + ( i + 1 ) );
c = c + "," + intScanner.nextInt();
co.add(c);
}

现在让他们回来只是简单地拆分,并使用Integer.valueOf()让他们回到int。

答案 2 :(得分:0)

扫描仪不能很好地处理2D​​数组,而是我们如何构建代码。让我们看一下 - 首先,您希望用户输入要评估的点数:

System.out.println( "Enter number of coordinates to evaluate: " );
points = intScanner.nextInt();

现在因为用户在数字后按ENTER键,输入中会添加一个换行符,所以为了使扫描器在循环中正常工作,首先,通过在紧接着后添加以下行来消耗换行符: / p>

//consumes newline character 
intScanner.nextLine();

接下来,如果要使用数组,请声明2D数组:

int[][] coords = new int[points][2];

和循环:

for(int i=0; i<points; i++){
    System.out.println( "Enter the x-coordinate for point " + ( i + 1 ) );
    coords[i][0] = intScanner.nextInt();

    //again, consume newline character added when user pressed ENTER
    intScanner.nextLine();

    System.out.println( "Enter the y-coordinate for point " + ( i + 1 ) );
    coords[i][1] = intScanner.nextInt();

    //and one more time, consume newline character added when user pressed ENTER
    intScanner.nextLine();
}

如果您愿意,也可以使用ArrayList。有几种方法可以实现:你可以存储字符串,例如。 “x,y”,或者您可以创建Point类并将其存储在ArrayList中,例如

public class Point {
    private int x;
    private int y;

    public Point(final int x, final int y) {
        this.x = x;
        this.y = y;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Point point = (Point) o;

        if (x != point.x) return false;
        if (y != point.y) return false;

        return true;
    }
}

然后,添加:

ArrayList<Point> coords = new ArrayList<Point>();

for (int i=0; i<points; i++) {
    System.out.println( "Enter the x-coordinate for point " + ( i + 1 ) );
    int x = scan.nextInt();

    scan.nextLine();

    System.out.println( "Enter the y-coordinate for point " + ( i + 1 ) );

    int y = scan.nextInt();

    scan.nextLine();

    //create Point object and add it to list
    coords.add(new Point(x, y));
}

然后您可以通过索引删除:

coords.remove(indexToRemove);

或者如果你知道Point coords:

//remove Point with coords x=1 and y=1
coords.remove(new Point(1,1));

要从列表中获取点,请使用:

Point p = coords.get(index)