返回ArrayList和在另一种方法中problm使用它 [英] Return ArrayList and use it in another method problm
问题描述
我有一个返回对象的ArrayList的方法。我返回数组列表创建的,它充满了许多对象,我检查他们在那里通过同样的方法打印。当我使用的ArrayList作为其他方法的参数,它给了我一个错误,就像如果数组列表是空的。
I have a method that returns an ArrayList of Objects. I return the arraylist created, and its filled with many objects, that I check they are there by printing them in the same method. When I use that Arraylist as parameter in another method, it gives me an error, like if the arraylist were empty.
可以说物体的位置(Y,X),用方法getFila和getColumna。
和失败的方法(实际上什么都不做):
Lets say the object its Position(y,x), with methods getFila and getColumna. And the method that fails (actually does nothing):
public static ArrayList<Position> getTrayectory (ArrayList<Position> aListPos){
for (int ii = 0; ii < aListPos.size();ii++ ) {
System.out.println("(" + aListPos.get(ii).getFila() + "," + aListPos.get(ii).getColumna()+")");
}
return aListPos;
}
我回到里面许多对象的数组列表,但我不知道什么是与该发生的事情。
I return the Arraylist with many objects inside, but I dont know what is happening with that.
摘要:
1- I have a method that returns an ArryList
2- I create a new Arraylist with method in 1
3- I use new method with Arraylist in 2 as parameter.
(4-) Nothing happens, because new arraylist in 2 look empty.
谁能帮我在这个过程中?我需要帮助,很快很快。
Can anyone help me in the process? I need that help very very soon.
推荐答案
如果您在(看不见的)方法,它提供了创建ArrayList中,要退呢?或者,你修改参数?
If you are creating the ArrayList in the (unseen) method that provides it, are you returning it? Or are you modifying a parameter?
在换句话说,你在做这样的:
In other words, are you doing this:
ArrayList<Position> myArrayList = generateArrayList(); // or whatever you call it
或你在做这样的:
ArrayList<Position> myArrayList;
generateArrayList(myArrayList);
ArrayList<Position> result = getTrayjectory(myArrayList);
这样做的第二个版本将无法正常工作,因为当你在方法内部创建一个对象,它可以逃脱的方法的边界的唯一方法是通过该方法返回。否则,超出范围时,该方法结束。
The second version of this won't work, because when you create an object inside a method, the only way that it can escape the method's bounds is to be returned by that method. Otherwise, it goes out of scope when the method ends.
这篇关于返回ArrayList和在另一种方法中problm使用它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!