[英]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.