我尝试了不同的变体,但我不知道如何让它工作。我试图让用户输入基于用户选择的数字进行循环的坐标,但扫描仪似乎不能很好地处理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]);
}
}}
发布于 2013-12-01 00:11:59
正如您声明的那样,coords是一个“ArrayLists数组的数组”类型的变量。为什么会这样呢?
您需要将coords声明为
int[][] coords = new int[points][2];然后简单地像下面这样赋值:
coords[i][0] = intScanner.next();好吧,我假设这是Java语言。如果不是,那么我的答案可能需要改进。
发布于 2013-12-01 00:16:32
现在我很可能是错的,但我不认为你理解二维数组是如何工作的。
网格中的二维数组单词类似于列和行的方式。你用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()将它们返回整数。
发布于 2013-12-01 00:39:02
这并不是说扫描器不能很好地处理2D数组,而是我们构造代码的方式。让我们来看看-首先,你想让用户输入要评估的点数:
System.out.println( "Enter number of coordinates to evaluate: " );
points = intScanner.nextInt();现在,由于用户将在数字后按ENTER键,因此将在输入中添加一个换行符,因此为了使扫描仪在循环中正常工作,首先,通过在后面紧跟以下行来使用换行符:
//consumes newline character
intScanner.nextLine();接下来,如果你想使用数组,声明二维数组:
int[][] coords = new int[points][2];and循环:
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);或者,如果您知道点坐标:
//remove Point with coords x=1 and y=1
coords.remove(new Point(1,1));要从列表中获取点,请使用:
Point p = coords.get(index)https://stackoverflow.com/questions/20302365
复制相似问题