如何让此程序将用户输入添加到我的链接列表中?

时间:2017-12-04 18:41:52

标签: java

import java.util.*;

public class TestClass {
static String name;
static String gender;
static String hairColor;

TestClass(String name, String gender, String hairColor){
    this.name=name;
    this.gender=gender;
    this.hairColor=hairColor;

}
public static void main (String[]args) {
    TestClass info = new TestClass(name, gender, hairColor);
    LinkedList<TestClass> linky = new LinkedList<TestClass>();
    Scanner input = new Scanner(System.in);

    System.out.println("Enter name ");
    name=input.nextLine();

    System.out.println("Enter gender ");
    gender=input.nextLine();

    System.out.println("Enter hair color ");
    hairColor=input.nextLine(); 

    }

}

我想将用户输入添加到链接列表中,但我无法弄清楚如何操作。我曾尝试使用linky.add(name);,但这会产生错误。如何将输入添加到链接列表中?

5 个答案:

答案 0 :(得分:1)

首先需要将所有用户输入存储在TestClass对象中,然后将该对象添加到LinkedList

从用户获取输入后,将包含用户输入值的所有变量传递给TestClass构造函数

TestClass test = new TestClass(name, gender, hairColor);

然后将test对象存储在LinkedList

linky.add(test); 

重要提示:

如果您要存储多个TestClass个实例,并且每个实例都有自己的数据,那么类static的数据成员变量不应该是TestClass

答案 1 :(得分:1)

您需要将TestClass的新实例添加到LinkedList,如下所示:

public static void main(String[] args) {
    LinkedList<TestClass> list = new LinkedList<>();
    Scanner input = new Scanner(System.in);

    System.out.println("Enter name ");
    String name = input.nextLine();

    System.out.println("Enter gender ");
    String gender = input.nextLine();

    System.out.println("Enter hair color ");
    String hairColor = input.nextLine(); 

    list.add(new TestClass(name, gender, hairColor));
}

答案 2 :(得分:1)

import java.util.*;

public class TestClass {
    // These should (most likely) not be static
    // Read up on what 'static' means
    String name;
    String gender;
    String hairColor;

    TestClass(String name, String gender, String hairColor){
        this.name = name;
        this.gender = gender;
        this.hairColor = hairColor;
    }

    public static void main (String[]args) {
        LinkedList<TestClass> linky = new LinkedList<TestClass>();

        Scanner input = new Scanner(System.in);

        System.out.println("Enter name ");
        String name = input.nextLine();

        System.out.println("Enter gender ");
        String gender = input.nextLine();

        System.out.println("Enter hair color ");
        String hairColor = input.nextLine();

        TestClass info = new TestClass(name, gender, hairColor);

        linky.add(info);
    }
}

由于linky是LinkedList<TestClass>,其泛型类型为TestClass,因此只能保存TestClass类型的对象,因此当您尝试linky.add(name)时它无法正常工作

要打印出列表的内容,您需要遍历列表并打印每个元素,如果列表包含String s或带有重写toString方法的内容,那么您可以简单地拨打:

for(String str : linky){
    System.out.println(str);
}

但是这不起作用,因为linky包含TestClass es。因此你可以这样做(但你应该为TestClass的字段定义get方法):

for(TestClass testClass : linky){
    System.out.println("Name: " + testClass.name);
    System.out.println("Gender: " + testClass.gender);
    System.out.println("Hair Color: " + testClass.hairColor);
}

TestClass您可以覆盖从其超类toString继承的Object方法

// In TestClass
@Override
public String toString(){
    // Format this however you like
    return "Name :" + this.name + "; Gender: " + this.gender + "; Hair Color: " + this.hairColor;
}

然后您需要做的就是:

for(TestClass testClass : linky){
    // println(testClass) calls testClass.toString()
    // giving you the formatted data from TestClass
    System.out.println(testClass);
}

答案 3 :(得分:0)

LinkedList<TestClass> linky = new LinkedList<TestClass>();

您需要添加TestClass

的实例

答案 4 :(得分:0)

只需改变它:

LinkedList<String> linky = new LinkedList<String>();
    Scanner input = new Scanner(System.in);

    System.out.println("Enter name ");
    name=input.nextLine();
    linky.add(0, name);
    System.out.println("Enter gender ");
    gender=input.nextLine();
    linky.add(1, gender);
    System.out.println("Enter hair color ");
    hairColor=input.nextLine(); 
linky.add(2, hairColor);