• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android App to loop through an Array list and return

revelution

Lurker
Oct 10, 2016
6
0
Hello Guys,
I'm trying to create a simple App to loop through an Array,from zero to end and
then loop from end to the initial
here is my code example:
String[] myList = {"Hello","How are you","Whats happening","Hope you are happy"};
int i = 0;

Then my onClick code;
while(i<=myList.length){
text.setText(view)
i++;
}

Finally i was able to loop through the Array from beginning to end,but Am unable to loop back.
 
Last edited:
Your code has potential to cause an ArrayIndexOutOfBounds exception. Because when i == myList.length, then myList[i] will throw the exception.

To iterate forwards through the list:

Code:
for (String str : myList) {
  // do something with str
}

To go backwards:

Code:
for (int i=myList.length-1; i >= 0; i--) {
  // do something with myList[i]
}
 
Last edited by a moderator:
Upvote 0
I'm not really understanding what you're trying to do. Instead of small code fragments, could you post up more of your code, and try to explain more clearly what you want to achieve. The code fragment above makes little sense to me.
Put code in [code][/code] tags please.
 
Upvote 0
I want a situation where my loop continue after the first loop through the whole array, then it will return back.

Code:
int x = myList.length;
int y = myList.length;

for (x=9;x<=10;x++){
     for (y=5;y <=x;y++){
            text.setText (view [x%y]);
     }
}
The above code worked on Netbeans Ide but doesn't work on android studio when i installed the app on my phone.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones