1

Hello this is my first post on Stackoverflow.. Sorry if this is duplicated question but i already search in google & stackover to find the solution but i got zero.

I have this problem in my project, all my value in ArrayList was replaced by last index value. And i create this new simple project to make easy to understand.

public class MainActivity extends AppCompatActivity {
    ArrayList<String> temp1 = new ArrayList<>();
    ArrayList <ArrayList<String>> tampung = new ArrayList <> ();
    String kata;
    TextView eks1,eks2,hasil1,hasil2,size;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        eks1 = (TextView) findViewById(R.id.ekspektasi1);
        eks2 = (TextView) findViewById(R.id.ekspektasi2);
        hasil1 = (TextView) findViewById(R.id.hasil1);
        hasil2 = (TextView) findViewById(R.id.hasil2);
        size = (TextView) findViewById(R.id.size);

        //add pertama
        temp1.clear();
        for (int c = 0; c < 5; c++) {
            kata = "ini kata ke " + (c+1);
            temp1.add(kata);
        }
        //Masuk ke tampungan array untuk index 0
        tampung.add(temp1);
        Log.d("Tampungan dalam 1", temp1.toString());
        eks1.setText("EKSPEKTASI 1 : "+temp1.toString());

        //data pertama dihapus ganti dengan data ke 2
        temp1.clear();
        //add data ke 2
        for (int c = 0; c < 5; c++) {
            kata = "ini " + (c+1);
            temp1.add(kata);
        }
        //masuk ke tampungan array untuk index 1
        tampung.add(temp1);
        Log.d("Tampungan dalam 1", temp1.toString());
        eks2.setText("EKSPEKTASI 2 : "+temp1.toString());

        Log.d("Tampungan luar 1",tampung.get(0).toString());
        hasil1.setText("HASIL 1 : " +tampung.get(0).toString());
        Log.d("Tampungan luar 2",tampung.get(1).toString());
        hasil2.setText("HASIL 2 : "+tampung.get(1).toString());
        Log.d("Size", String.valueOf(tampung.size()));

        size.setText("SIZE DARI TAMPUNGAN ARRAY : "+tampung.size());
    }
}

And this is the result ( i copied it from logcat )

09-02 16:12:42.444 15418-15418/com.example.siwonhansamu.tolongcoba D/Tampungan dalam 1: [ini kata ke 1, ini kata ke 2, ini kata ke 3, ini kata ke 4, ini kata ke 5]
09-02 16:12:42.444 15418-15418/com.example.siwonhansamu.tolongcoba D/Tampungan dalam 2: [ini 1, ini 2, ini 3, ini 4, ini 5]
09-02 16:12:42.444 15418-15418/com.example.siwonhansamu.tolongcoba D/Tampungan luar 1: [ini 1, ini 2, ini 3, ini 4, ini 5]
09-02 16:12:42.444 15418-15418/com.example.siwonhansamu.tolongcoba D/Tampungan luar 2: [ini 1, ini 2, ini 3, ini 4, ini 5]
09-02 16:12:42.444 15418-15418/com.example.siwonhansamu.tolongcoba D/Size: 2

Thanks :D

Jitesh Dalsaniya
  • 1,917
  • 3
  • 20
  • 36
  • The values look correct to me. – Amad Yus Sep 02 '16 at 09:28
  • You are adding the same object twice to your "tampung" list. So tampung.get(0) and tampung.get(1) point to the same list (temp1). I think what you are trying to do is add two different list. don't use .clear() on temp1, instead create a new object and add it to your main list. But your question is quite unclear, so i hope i understood you correct. – OH GOD SPIDERS Sep 02 '16 at 09:32
  • @911DidBush is right. This is the problem about the arraylist. Item inside the arraylist is not easily be removed if there is a nested arraylist inside it since list in java is immutable. See my response here http://stackoverflow.com/questions/38966252/how-to-correctly-initialize-a-list-member-object-in-java/38966478#38966478 – Long Ranger Sep 02 '16 at 10:03

3 Answers3

1

Thanks for the answer, finally i accidentally got the answer

i just need to add new Arraylist every i want to insert my arraylist to Arraylist>

tampung.add(new Arraylist<String>(temp1));
0

this is because when you clear the temp1 arraylist, value in the tampung array clear. so don't clear the temp1 array after adding temp1 into tampung .

  //add pertama
    temp1.clear();
    for (int c = 0; c < 5; c++) {
        kata = "ini kata ke " + (c + 1);
        temp1.add(kata);
    }
    //Masuk ke tampungan array untuk index 0
    tampung.add(temp1);
    Log.d("Tampungan dalam 1", temp1.toString());


    //data pertama dihapus ganti dengan data ke 2
    ArrayList<String> temp1 = new ArrayList<>();
    //add data ke 2
    for (int c = 0; c < 5; c++) {
        kata = "ini " + (c + 1);
        temp1.add(kata);
    }
    //masuk ke tampungan array untuk index 1
    tampung.add(temp1);
    Log.d("Tampungan dalam 1", temp1.toString());


    Log.d("Tampungan luar 1", tampung.get(0).toString());

    Log.d("Tampungan luar 2", tampung.get(1).toString());

    Log.d("Size", String.valueOf(tampung.size()));
Pawanpreet
  • 355
  • 1
  • 4
  • 13
0

instead of temp1.clear() you have to use temp1=new ArrayList(); Your problem will be resolved

Rahul Chaudhary
  • 1,059
  • 1
  • 6
  • 15