简体   繁体   English

如何执行带有循环的ArrayList程序,该循环一次呈现列表的每个索引?

[英]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? 只要数组/列表[idk要使用哪一个,但我被告知使用ArrayList不必预先定义大小,我将使用该大小],那么我将如何执行程序,它将继续循环并一次观察或呈现一个值?

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循环,但是基本上我所得到的只是请求数组或说不兼容类型之类的错误。

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. 看来您是根据C指针来考虑它的。 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. 如果您使用ArrayList或List接口的其他实现,则不会使用方括号或括号直接访问数据,这意味着说myList [0]之类的内容是没有意义的。 Instead, what you are going to want to do is use the get method and then compare it to whatever values you need. 相反,您要使用的是get方法,然后将其与所需的任何值进行比较。 You will be able to get the length of your List by using the .size() method, so your code will look something like this 您可以使用.size()方法获取列表的长度,因此您的代码将如下所示

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. glowcoder给出的响应是用于遍历Java中的ArrayList和其他List的更紧凑的语法,并产生了更好的代码,但这是在没有语法糖的情况下使用的方式。

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. 请注意,这不是遍历列表的首选方式,有关更优雅的信息,请参见glowcoder的答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在List的每个索引上创建ArrayList - Making ArrayList on each index of List Java ArrayList,如何一次列出一个? - Java ArrayList, how to list one at a time? 如何向此驱动程序添加一个构造函数,以便它可以同时使用arraylist和链表? - How do I add one constructor to this driver program so that it works with both arraylist and linked list? 获取迭代ArrayList的每个循环的当前索引 - Get the current index of a for each loop iterating an ArrayList 每次循环遍历列表或删除元素是否更有效,因为我在 Java 中找到了我想要的元素? - Is it more efficient to loop over a list each time or remove elements as I find the one I want in Java? 使用Java Loop for ArrayList每次打印 - using Java Loop for ArrayList to print each time 如何将不同对象的 ArrayList 组合到一个列表 / Map 中,然后从组合列表 /Map 中检索每个 arraylist 的值? - How to combine ArrayList of different objects into one list / Map and then retrive value of each arraylist from combined list /Map? 我如何访问用于每个循环的另一个类中的 ArrayList - How do i access ArrayList that is in another class using for each loop 在 Java 中,如何创建一个 ArrayList,该列表中的一个元素是另一个 ArrayList,每个元素都是唯一的? - In Java, how do you create an ArrayList, with one element of that list being another ArrayList, that is unique for each element? 如何为数据库表中的每个记录生成ID? - How shall I generate an id for each record in a database table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM