I tried to assign one ArrayList to another by using:
ArrayList<type>list1 = this.reSetItemList() //this method return a arraylist
and
ArrayList<type>list1 = new ArrayList<>(this.reSetItemList());
and
ArrayList<type>list1.addAll(this.reSetItemList())
But all these three ways store nothing into list1. Could someone help me out? A lot of thanks!
Here is my code:
public calss Field{
ArrayList<Item> itemList = new ArrayList<>(this.reSetItemList());
public ArrayList<Item> getItemList(){ //check if itemList is empty
return itemList;
}
public ArrayList<Item> reSetItemList() { //initialize the field
ArrayList<Item> arr = new ArrayList<Item>();
for(int i = 0; i < this._area; i++) {
arr.add(new Soil());
}
return arr;
}
public Field(int height, int width) {
_height = height;
_width = width;
_area = _width * _height;
for(int i = 1; i < field.length; i++) {
for (int j = 1 ; j < field[i].length; j++) {
field[i][j] = itemList.get(i);
}
}
}
mian:
Field testField [2,2] = new Field;
print(testField.reSetItemList()) //will print".,.,.,."
print(testField.getItemList() //will print nothing
is that because I put the code in the wrong line? The full code