0

I'm trying to create a LinkedList of a custom class.

I've stripped my code back and followed the suggestion here:

arraylist with custom objects

When I run the code and debug it, I can see that the LinkedList number of elements is increasing with each add, but all the data is 'sitting' in the first index, with the later elements all empty.

public class editTextString {
        private String  str1;
        private String  str2;
        private String  str3;

        public editTextString(String data1,String data2,String data3){

        str1 = data1;
        str2 = data2;
        str3 = data3;
}


public static void main(String[] args) {
    ArrayList<editTextString> list = new ArrayList<editTextString>();
    editTextString data = new editTextString("edit1","edit2","edit3");
    list.add(data);
    list.add(data);
    list.add(data);
}

Expected output: List to have 3 elements each with three substrings within. Actual output: List with 3 elements, first contains three substrings, next two are empty.

2
  • 1
    Please show a complete example. As each of the elements in your list is the same object it isn't possible for the first to have three strings, and the next two to be empty. Commented Jun 27, 2023 at 8:29
  • 1
    How you know that "next two are empty." ? (For example, is the output of printing list.get(1).str1?) Commented Jun 27, 2023 at 9:26

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.