简体   繁体   中英

How shall I execute an ArrayList program with a loop that renders each index of the list one at a time?

How shall I execute a program where as long as the array/list [idk which one to use yet but I've been told that with ArrayList I don't have to predefine a size so I'll use that] it will keep looping and the values are observed or rendered one at a time?

I've tried using the while loop but basically all I got were errors asking for an array or saying incompatible types or something like that.

while (myList!=0) //Can I actually do this? Because I didn't define a data type for my list.

if myList(0).equals ("A") //problem here is that I need to go through every index of the list. I've tried to use a counter like if myList(counter).equals ("A") but it says its incompatible types?
{
//print something.
} else if myList(0).equals ("B")
{
//print something
}

I know the question is kinda confusing but the code [even if it's full of errors] is exactly what I want to do. I just don't know how to apply it. Any help, answers, links, articles, tutorials would be reaaaaly appreciated.

Looks like you're thinking of it in terms of a C pointer. The List always points to a List, never to a Node or anything like that. So you'd do something like this intsead:

for(String s : myList) {
    if("A".equals(s)) {

    } else if("B".equals(s)) {

    }
}

If you're using an ArrayList or some other implementation of the List interface you aren't going to have direct access to the data using brackets or parentheses, meaning that saying something like myList[0] is meaningless. Instead, what you are going to want to do is use the get method and then compare it to whatever values you need. You will be able to get the length of your List by using the .size() method, so your code will look something like this

for(int i=0; i<myList.size(); i++)
{
  if(myList.get(i).equals("A"))
   {
     //print something
   }
  else if(myList.get(i).equals("B"))
   {
     //print something else
   }
  else
   {
      //print something still different
   }
 }

The response that glowcoder gave is a more compact syntax for looping over ArrayLists and other Lists in Java and results in nicer code, but this is how you'd do it sans syntactic sugar.

Is this answer suitable for your question?

for (int i = 0; i < myList.length; i++) {
    if ("A".equals(myList[i])) {
        // print something.
    } else if ("B".equals(myList[i])) {
        // print something
    }
}

This should be working and quite close to what you tried so far:

int counter = 0;
while(counter < myList.size()) {
  if (myList.get(counter).equals("A") )
  {
  //print something.
  } else if (myList.get(counter).equals("B"))
  {
  //print something
  }
  counter = counter + 1;
}

Note that this is not the preferred way of looping over a list, see glowcoder's answer for something more elegant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM