Java:创建一个类的列表(List<Class>),并遍历它
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33112088/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Java: Create a list of a class (List<Class>), and iterate through it
提问by Mark Buffalo
I'm coming from a C# background, and I'm trying to figure out how to use classes the way I used them in C#. If this is notthe way to use them in Java, I'd like to know the correctway to do it.
我来自 C# 背景,我试图弄清楚如何像在 C# 中使用它们一样使用类。如果这不是在 Java 中使用它们的方法,我想知道正确的方法。
Basically, here's what I want to do:
基本上,这就是我想要做的:
- Create a List and fill it with data.
- Iterate through said List
- 创建一个列表并用数据填充它。
- 遍历上述列表
Here's my attempt thus far:
这是我迄今为止的尝试:
The is the class object I want:
public class ClassName { public ClassName(String _str, int _a, int _b, int _c, int _d, long _e) { str = _str; a = _a; b = _b; c = _c; d = _d; e = _e; } public String str; public int a; public int b; public int c; public int d; public long e; }
This is the way it's referenced in the main class file:
// Why would I use this instead of List<T>? ArrayList<ClassName> hList = new ArrayList<ClassName>();
This is how I'm attempting to fill the class object:
hList.add(new ClassName("string", 1, 2, 3, 4, 2432342322));
这是我想要的类对象:
public class ClassName { public ClassName(String _str, int _a, int _b, int _c, int _d, long _e) { str = _str; a = _a; b = _b; c = _c; d = _d; e = _e; } public String str; public int a; public int b; public int c; public int d; public long e; }
这是它在主类文件中引用的方式:
// Why would I use this instead of List<T>? ArrayList<ClassName> hList = new ArrayList<ClassName>();
这就是我试图填充类对象的方式:
hList.add(new ClassName("string", 1, 2, 3, 4, 2432342322));
This is the error I'm getting in Eclipse:
这是我在 Eclipse 中遇到的错误:
The constructor ClassName(String, int, int, int, int, long) is undefined
The constructor ClassName(String, int, int, int, int, long) is undefined
...which is baffling. Eclipse asks me to add what I've alreadyadded. When I choose the "quick fix" option, it does the same thing as the constructor above, but without the type = type
stuff. Maybe I'm not handling this correctly?
......这令人费解。Eclipse 要求我添加我已经添加的内容。当我选择“快速修复”选项时,它与上面的构造函数做同样的事情,但没有这些type = type
东西。也许我没有正确处理这个?
Assuming this is the right way to iterate, here's what I'd assume would work:
假设这是迭代的正确方法,我认为这是可行的:
for (int i = 0; i < hList.size(); i++)
{
System.out.println(hList[i].currentDate);
}
So, how do I create a proper list and iterate through it in Java?
那么,我如何创建一个合适的列表并在 Java 中遍历它呢?
回答by John Kugelman
I
一世
public ClassName(string _str, int _a, int _b, int _c, long _d)
{
str = _str;
a = _a;
b = _b;
c = _c;
d = _d;
}
Naming with underscores isn't so common. I recommend using this
instead. Also, String
is capitalized, and curly braces are typically at the end of lines.
用下划线命名并不常见。我建议this
改用。此外,String
大写,并且花括号通常位于行尾。
public ClassName(String str, int a, int b, int c, long d) {
this.str = str;
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
II
二
// Why would I use this instead of List<T>?
ArrayList<ClassName> hList = new ArrayList<ClassName>();
A more generic type like List
, Collection
, or Iterable
is better on the lefthand side, same as in C#. I recommend against Hungarian notation: list
is preferred over hList
. Additionally, you can omit the right side type name and use the diamond operator(<>
) to infer the type.
更通用的类型,如List
, Collection
, 或Iterable
在左侧更好,与 C# 中相同。我建议不要使用匈牙利符号:list
优于hList
. 此外,您可以省略右侧的类型名称并使用菱形运算符( <>
) 来推断类型。
List<ClassName> list = new ArrayList<>();
III
三
The constructor
ClassName(String, int, int, int, int, long)
is undefined.
构造函数
ClassName(String, int, int, int, int, long)
未定义。
Your constructor has three int
s and a long
; you're trying to pass four int
s and a long
.
您的构造函数具有三个int
s 和一个long
; 你试图通过四个int
s 和 a long
。
IV
四
Assuming this is the right way to iterate, here's what I'd assume would work:
for (int i = 0; i < hList.size(); i++) { System.out.println(hList[i].currentDate); }
假设这是迭代的正确方法,我认为这是可行的:
for (int i = 0; i < hList.size(); i++) { System.out.println(hList[i].currentDate); }
A for each loop is better if you don't need the index variable i
.
如果您不需要索引变量,则 for each 循环会更好i
。
for (ClassName item: list) {
System.out.println(item.currentDate);
}
If you do want i
then change [i]
to .get(i)
. Java doesn't have operator overloading, so []
is only available on arrays, not classes or interfaces like List
.
如果您确实想要,i
则更[i]
改为.get(i)
. Java 没有运算符重载,因此[]
只能用于数组,不能用于类或接口,如List
.
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i).currentDate);
}
回答by Isaac Schneider
You're adding an extra int in the constructor of your class, it takes 1 String, 3 ints and 1 long, and you're trying to create it with 1 String, 4 ints and 1 Long. And the way of iterate your list is
您在类的构造函数中添加了一个额外的 int,它需要 1 个 String、3 个 int 和 1 个 long,并且您试图用 1 个 String、4 个 int 和 1 个 Long 创建它。迭代你的列表的方式是
for (ClassName element:hList){
//something meaningful here
System.out.println(element.currentDate);
}
You could also use a Java 8 feature :)
您还可以使用 Java 8 功能:)
hList.forEach(className -> {
System.out.println(className.currentDate);
});