我不知道如何获取通用列表实例的特定项目。假设我有这样的东西;
public class Column {
private String name;
private float width;
public Column(String name, float width) {
this.name=name;
this.width=width;
}
public String getName() {
return name;
}
还有另一个类;
public class WriteColumn {
private List<Column> col = new ArrayList<>();
public void addColumn() {
col.add(new Column("yo", 0.1f));
col.add(new Column("mo", 0.3f));
writeColumn(col);
public void writeColumn(List<Column> col) {
String str = "";
for (Column col1 : col) {
str += col1 + " - ";
}
System.out.println("Cols: " + str);
}
public static void main(String[] args) {
WriteColumn wc = new WriteColumn();
wc.addColumn();
}
}
我想要得到的输出是该列的文本部分,但我没有得到它。有简单的方法吗?
最佳答案
我不明白为什么你不能使用 getName() 方法?
它应该有效:
public void writeColumn(List<Column> col) {
String str = "";
for (Column col1 : col) {
str += col1.getName() + " - ";
}
System.out.println("Cols: " + str);
}
关于java - 如何获取通用列表实例的特定项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26910117/