0

I have a next loop:

arrayI = new POJO_I [response.body().length];
arrayI = response.body();
arrayOfNamesMain = new String[10];

for(int t = 0; t< arrayI.length; t++) {
    if (arrayI[t].getMain().equals("1")) {
        arrayOfNamesMain[t] = arrayI[t].getName();
    }
}

After loop ending, I have arrayOfNamesMain with a few first empty elements, so actually it starts from the 5th index because first positions of arrayI[t] are not in if statement.

What should I do for filling my arrayOfNamesMain from 0 indexes? How can I sort the array and remove empty elements?

3
  • You're asking three different things in this question. I answered the one I think it's the post about, but please make sure to make clear, concise and single questions per post. Also your title's not very good. Commented Nov 6, 2019 at 10:14
  • 2
    Why are you initializing arrayI twice? Commented Nov 6, 2019 at 10:22
  • @TaslimOseni, no ideas :) fixed it. thanks. Commented Nov 6, 2019 at 10:35

3 Answers 3

1

Fill your arrayOfNamesMain using a different counter.

arrayI = new POJO_I [response.body().length];
int counter = 0;
arrayI = response.body();
arrayOfNamesMain = new String[10];

for(int t = 0; t< arrayI.length; t++) {
    if (arrayI[t].getMain().equals("1")) {
        arrayOfNamesMain[counter] = arrayI[t].getName();
        counter++;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1
arrayI = new POJO_I [response.body().length];
arrayI = response.body();
arrayOfNamesMain = new String[10];
int i = 0;
for(int t = 0; t< arrayI.length; t++) {
    if (arrayI[t].getMain().equals("1")) {
        arrayOfNamesMain[i] = arrayI[t].getName();
        i++;
     }
}

Comments

1

use a different counter variable inside if block which starts from 0 and increment it inside if block if conditions satisfy... it will solve your problem. int j = 0; arrayOfNamesMain[j] = arrayI[t].getName();

Comments

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.