Java ArrayList get() 方法不return 指向对象?
Java ArrayList get() method doesn't return Point object?
所以我创建了一个 Point 对象的 ArrayList,但是当我使用 ArrayList 的 get() 方法时,它似乎没有 return Point 对象。为什么会这样?
public class SkylineDC {
public static void openAndReadFile(String path,ArrayList pointsList){
//opening of the file
Scanner inputFile=null;
try{
inputFile=new Scanner(new File(path));
}
catch (Exception e1){
try{
inputFile=new Scanner(new File(path+".txt"));
}
catch (Exception e2){
System.out.println("File not found..");
System.exit(1);
}
}
//reading of the file
short listSize=inputFile.nextShort();
pointsList=new ArrayList<Point>(listSize);
//pointsList.add(new Point(10,10));
//System.out.println("Size of List:"+pointsList.size());
pointsList.get(0).
}
public static void main(String[] args) {
String path=args[0];
ArrayList totalPoints=null;
openAndReadFile(path,totalPoints);
//System.out.println("finish");
}
}
ArrayList pointsList
您在声明中使用了原始类型。
改为ArrayList<Point> pointsList
还有:
ArrayList totalPoints=null
也不应该是原始类型。更改为 ArrayList<Point> totalPoints = new ArrayList<>()
我还建议简单地使用您的方法 return List<Point>
而不是尝试填充现有列表。我什至不知道您当前拥有的代码是否会像预期的那样运行。我怀疑它不会。
所以我创建了一个 Point 对象的 ArrayList,但是当我使用 ArrayList 的 get() 方法时,它似乎没有 return Point 对象。为什么会这样?
public class SkylineDC {
public static void openAndReadFile(String path,ArrayList pointsList){
//opening of the file
Scanner inputFile=null;
try{
inputFile=new Scanner(new File(path));
}
catch (Exception e1){
try{
inputFile=new Scanner(new File(path+".txt"));
}
catch (Exception e2){
System.out.println("File not found..");
System.exit(1);
}
}
//reading of the file
short listSize=inputFile.nextShort();
pointsList=new ArrayList<Point>(listSize);
//pointsList.add(new Point(10,10));
//System.out.println("Size of List:"+pointsList.size());
pointsList.get(0).
}
public static void main(String[] args) {
String path=args[0];
ArrayList totalPoints=null;
openAndReadFile(path,totalPoints);
//System.out.println("finish");
}
}
ArrayList pointsList
您在声明中使用了原始类型。
改为ArrayList<Point> pointsList
还有:
ArrayList totalPoints=null
也不应该是原始类型。更改为 ArrayList<Point> totalPoints = new ArrayList<>()
我还建议简单地使用您的方法 return List<Point>
而不是尝试填充现有列表。我什至不知道您当前拥有的代码是否会像预期的那样运行。我怀疑它不会。