Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取通用列表维护';s具体项目_Java_List_Generics - Fatal编程技术网

Java 如何获取通用列表维护';s具体项目

Java 如何获取通用列表维护';s具体项目,java,list,generics,Java,List,Generics,我不知道如何为泛型列表实例获取特定项。假设我有这样的东西 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 WriteColu

我不知道如何为泛型列表实例获取特定项。假设我有这样的东西

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();
}
}
公共类WriteColumn{
private List col=new ArrayList();
public void addColumn(){
列添加(新列(“yo”,0.1f));
列添加(新列(“mo”,0.3f));
书面列(col);
public void writeColumn(列表列){
字符串str=“”;
for(列col1:col){
str+=col1+“-”;
}
System.out.println(“Cols:+str”);
}
公共静态void main(字符串[]args){
WriteColumn wc=新的WriteColumn();
wc.addColumn();
}
}

我想要得到的输出是列的文本部分,但我没有得到。有简单的方法吗?

我不明白为什么不能使用getName()方法

它应该在以下方面发挥作用:

public void writeColumn(List<Column> col) {
  String str = "";
  for (Column col1 : col) {
    str += col1.getName() + " - "; 
  }
  System.out.println("Cols: " + str);
}
public void writeColumn(列表列){
字符串str=“”;
for(列col1:col){
str+=col1.getName()+“-”;
}
System.out.println(“Cols:+str”);
}

我不明白为什么不能使用getName()方法

它应该在以下方面发挥作用:

public void writeColumn(List<Column> col) {
  String str = "";
  for (Column col1 : col) {
    str += col1.getName() + " - "; 
  }
  System.out.println("Cols: " + str);
}
public void writeColumn(列表列){
字符串str=“”;
for(列col1:col){
str+=col1.getName()+“-”;
}
System.out.println(“Cols:+str”);
}

以下代码正在工作,输出: 科尔斯:哟-莫-

我想这就是你所期待的

package com.vipin.test;

import java.util.*;

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.getName() + " - "; //used getName()
        }
        System.out.println("Cols: " + str);
    }


    public static void main(String[] args) {
        WriteColumn wc = new WriteColumn();
        wc.addColumn();
    }
}
package com.vipin.test;
导入java.util.*;
类列{
私有字符串名称;
私有浮动宽度;
公共列(字符串名称、浮动宽度){
this.name=name;
这个。宽度=宽度;
}
公共字符串getName(){
返回名称;
}
}
公共类WriteColumn{
private List col=new ArrayList();
public void addColumn(){
列添加(新列(“yo”,0.1f));
列添加(新列(“mo”,0.3f));
书面列(col);
}
public void writeColumn(列表列){
字符串str=“”;
for(列col1:col){
str+=col1.getName()+“-”;//使用的getName()
}
System.out.println(“Cols:+str”);
}
公共静态void main(字符串[]args){
WriteColumn wc=新的WriteColumn();
wc.addColumn();
}
}

以下代码正在工作,输出: 科尔斯:哟-莫-

我想这就是你所期待的

package com.vipin.test;

import java.util.*;

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.getName() + " - "; //used getName()
        }
        System.out.println("Cols: " + str);
    }


    public static void main(String[] args) {
        WriteColumn wc = new WriteColumn();
        wc.addColumn();
    }
}
package com.vipin.test;
导入java.util.*;
类列{
私有字符串名称;
私有浮动宽度;
公共列(字符串名称、浮动宽度){
this.name=name;
这个。宽度=宽度;
}
公共字符串getName(){
返回名称;
}
}
公共类WriteColumn{
private List col=new ArrayList();
public void addColumn(){
列添加(新列(“yo”,0.1f));
列添加(新列(“mo”,0.3f));
书面列(col);
}
public void writeColumn(列表列){
字符串str=“”;
for(列col1:col){
str+=col1.getName()+“-”;//使用的getName()
}
System.out.println(“Cols:+str”);
}
公共静态void main(字符串[]args){
WriteColumn wc=新的WriteColumn();
wc.addColumn();
}
}
col1.getName()
是可能的。
col1.getName()
是可能的。