0

i know this is sth really easy to do for many of you, but i have been trying all the answers here and everywhere else... none of them worked ..to me.. maybe you can help with this..? i have an ArrayList (keys) ... and i want to increment the index at each call... with a button.. i want the first out put to be: "key" : 0, then, pressing the button again and:"key" : 1, and again "key" : 2. Thank you in advance!! I did this:

for (int i = 0; i < keys.size(); i++) {
    if (like) {
        Log.i("key", keys.get(i));
        keys.set(i, keys.get(i) + 1);
    }
}
Mustafa Poya
  • 2,615
  • 5
  • 22
  • 36
just Jon
  • 13
  • 1
  • 6
  • what is the value of `like`? maybe that condition cause to prevent from increasing value – Mustafa Poya Dec 11 '20 at 14:37
  • i thought of it too .. 0,1,2,3,4,5 ... i know ints are immutable..thats why im trying to replace the value, but i dont know if i should crate a variable and then, replace it into the keys.get(i)+ "the new variable"---- thanks for replying Mustafa.. im very new at all this..thank you! – just Jon Dec 11 '20 at 14:50
  • so does your problem has been solved? – Mustafa Poya Dec 11 '20 at 14:51
  • not yet @MustafaPoya im trying everything here :) – just Jon Dec 11 '20 at 14:54
  • 1
    try to add the code of the `button` in your question for better help – Mustafa Poya Dec 11 '20 at 14:55
  • i will @MustafaPoya but....... its just : btn.setOnClickListener(new View.OnClickListener() { nothing weird..tho – just Jon Dec 11 '20 at 14:58
  • Does Log.i("key", keys.get(i)); add logs. How are you verifying or debugging whether your code worked correctly or not. I think you are debugging with the Log.i("key", keys.get(i)); whether the code executed correctly or not then it is completely false because this is executing before to the increment step. Please look into this. – Sarath Chandra Dec 11 '20 at 15:21
  • @sarathchandra dont get pissed with me.. im new in all this.. im here to learn more, i wasnt born knowing, i will follow your recommendations, thank you! – just Jon Dec 11 '20 at 15:33
  • @JonathanCheli I am just asking how are you verifying that's it. just write a System.out.println(keys.get(i)); after keys.set(i, keys.get(i) + 1); and paste the output. I think it will get incremented – Sarath Chandra Dec 11 '20 at 16:07

3 Answers3

0

You can try setting a global variable for index and then try to log and set the value in keys in click listener. Is this what you are trying to achieve?

int index = 0;
btn.setOnClickListener(new View.OnClickListener() { 
    Log.i("key: ", index);
    index++;
    keys.set(i, index);
});
rya
  • 1,417
  • 1
  • 16
  • 29
  • 2
    @MustafaPoya SE uses something called "caching", which means it's perfectly possible to miss answers being posted. Accusing people of plagiarism on an answer that's under a minute apart is not okay. On new questions, identical answers can often show up right after each other. That doesn't mean this was copied. – Zoe Dec 11 '20 at 15:12
0
for (int i = 0; i < keys.size(); i++) {
    if (like) {
        Log.i("key", keys.get(i));
        int temp = keys.get(i);
        ++temp;
        keys.set(i, temp);
    }
}

or

for (int i = 0; i < keys.size(); i++) {
    if (like) {
        Log.i("key", keys.get(i));
        keys.set(i, (keys.get(i)) + 1);
    }
}

any of the above 2 code snippets should solve your problem and even your code snippet is correct. can you please share more info about how you are verifying.

-1
Log.i("key", keys.get(i++));

should do the trick

Zoe
  • 27,060
  • 21
  • 118
  • 148
Indycate
  • 11
  • 3
  • Ill give it a try and ill let you know.. Indycate :) – just Jon Dec 11 '20 at 14:28
  • and no...not happening.. im still getting 0... i dont get it – just Jon Dec 11 '20 at 14:29
  • @JonathanCheli See https://stackoverflow.com/a/24858 - also note that this answer doesn't work, as it increments the key instead of the value. At about keys.size() / 2, this will throw an IndexOutOfBoundsException. Also, AFAIK, Java doesn't let you use prefix or postfix `++` on maps, arrays, lists, sets, or other similar constructs – Zoe Dec 11 '20 at 15:20
  • @Zoe super cool post, yes it did increment the key but not the value..as u mentioned.. thank so much for this post :) – just Jon Dec 11 '20 at 15:28