0

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

Rob
  • 320
  • 1
  • 10
Jan Zheng
  • 1
  • 2
  • 1
    I suggest posting more of the used code, since you're using the default constructor for example, and we don't know what it does and what's initialized how – Tidemor Mar 26 '22 at 21:35
  • This code is broken: `Field testField [2,2] = new Field; print(testField.reSetItemList()` "testField" is an array, and you don't access an array element when you call `print(testField.reSetItemList()` I don't think this is what you are actually doing. – markspace Mar 26 '22 at 21:45
  • Sorry, the drive code is: `Field testField = new Field(5, 5); System.out.println(testField.reSetItemList());` and it will print 25 "." – Jan Zheng Mar 26 '22 at 21:56

0 Answers0