Reputation: 175
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
Upvotes: 1
Views: 1100
Reputation: 1061
instead of temp1.clear() you have to use temp1=new ArrayList(); Your problem will be resolved
Upvotes: 0
Reputation: 175
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));
Upvotes: 1
Reputation: 355
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()));
Upvotes: 0