【问题标题】:Java ArrayList get() method doesn't return Point object?Java ArrayList get() 方法不返回 Point 对象?
【发布时间】:2018-03-21 18:26:00
【问题描述】:

所以我创建了一个 Point 对象的 ArrayList,但是当我使用 ArrayList 的 get() 方法时,它似乎没有返回 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");
    }

}

【问题讨论】:

    标签: java object arraylist get point


    【解决方案1】:

    ArrayList pointsList

    您在声明中使用了原始类型。

    改成ArrayList&lt;Point&gt; pointsList

    还有:

    ArrayList totalPoints=null 也不应该是原始类型。改为ArrayList&lt;Point&gt; totalPoints = new ArrayList&lt;&gt;()

    我还建议让您的方法返回 List&lt;Point&gt;,而不是尝试填充现有列表。我什至不知道您当前拥有的代码是否会按预期运行。我怀疑它不会。

    【讨论】:

    • 非常感谢!你是对的,它不像那样工作,但由于在 Java 中参数是通过引用传递的,为什么它不起作用?
    • 因为您在方法中重新分配了 list 变量。
    猜你喜欢
    • 1970-01-01
    • 2012-11-01
    • 2015-01-29
    • 2015-05-15
    • 2011-04-22
    • 1970-01-01
    • 2015-11-15
    • 2011-05-11
    • 2013-11-28
    相关资源
    最近更新 更多