0

Can anyone tell me how to add a new item to an index of a 2 dimensional ArrayList. The links click here do not resolve my problem.

My code to add was the following:

wq.get(donsay).set(wq.get(donsay).size(), sil5);

which causes the following exception =>

java.lang.RuntimeException: Unable to start activity ComponentInfo{chessactivesekizsatirvetekliarray.com.sekizsatirvetekliarray/chessactivesekizsatirvetekliarray.com.sekizsatirvetekliarray.MainActivity}: java.lang.StringIndexOutOfBoundsException: length=2; index=3

Thank you all in advance...

Community
  • 1
  • 1
Otag
  • 141
  • 11
  • 1
    Can you share more code? It's tough to tell what you're trying to accomplish without knowing if `wq` is a map or list or ? also what type are `donsay` and `sil5`? – randal4 Dec 29 '15 at 03:09
  • Hello, quite right.. I assumed that everyone knows what I am doing. 'donsay' is an integer and is derived from the first syllables of the phrase 'loop number' in my language. sil5 is a String. I am trying to add sil5(String) to index 'donsay'(integer) at wq's (a string ArrayList) donsay index length (which is a number). It is supposed to extend this index by one element considering index 2 => 0,1 but length 2 => 0,1,2 Pls let me know if everything is clear. Thank you – Otag Dec 29 '15 at 03:20

1 Answers1

0

set() method of ArrayList expects an index less than size. Use add() method instead:

wq.get(donsay).add(wq.get(donsay).size(), sil5);
Rediska
  • 1,392
  • 10
  • 14
  • I think this is the answer I am looking for. I'll check in a few mintes and let you know... Regards and thank you – Otag Dec 29 '15 at 03:27
  • 1
    In this case, no need to calculate the index, you can just do `wq.get(donsay).add(sil5)` – randal4 Dec 29 '15 at 04:28