从用户输入中读取泛型类型

时间:2014-02-25 04:30:57

标签: java arrays generics

我做了一些研究而没有发现任何内容,所以我需要帮助从用户输入中读取泛型类型。比方说我想输入一个整数,然后当我再次运行程序时,我想输入一个字符串。我如何使用扫描仪或其他输入阅读器?继承我的代码

public class array <T,E> {
private T [] a;
private int size;
private int location;
private E add;
private E delete;
private E find;

public array(){

}
public array(int size){
    this.size = size;
    this.location = 0;
    this.a = (T[])(new Object[size]);
}
public boolean add(T element){
    if(location == size){
        return false;
    }else{
        this.a[location++] = element;
        System.out.println("Element added at location " + location);
        return true;
    }
}
public int find(T element){
    for(int i = 0 ; i < location; i ++){
        if(a[i] == element){
            System.out.println("Element found at location " + i);
            return i;
        }
    }
    System.out.println("Couldn't find element");
    return -1;
}
public boolean delete(T element){
    int loc = find(element);
    if(loc == -1){
        System.out.println("Couldn't find element");
        return false;
    }else{
    for(int x = loc; x < location - 1; x++){
        a[x] = a[x+1];
    }
        System.out.println("Element deleted");
        location -= 1;
    return true;
    }
}
public void go(){
        DataInputStream in = new DataInputStream(System.in);
        Scanner scanner = new Scanner(System.in);
        array a = null;
        int choice = 0;
        System.out.println("Chose what you want to do");
        System.out.println("1: Add a value");
        System.out.println("2: Delete a value");
        System.out.println("3: Find a value and return the index");
        System.out.println("4: Display all elements in array");
        System.out.println("5: Exit");
        System.out.println();
        try{
        choice = scanner.nextInt();
        }catch(Exception e){
            System.out.println("Incorrect input");
            go();
        }
        switch(choice){
            case 1:
                System.out.println("Enter the element you want to add");
                add = in.read();
                a.add(add);
                break;
            case 2: 
                System.out.println("Enter the element you want to delete");
                delete = in.nextInt();
                a.delete(delete);
                break;
            case 3:
                System.out.println("Enter the element you want to find");
                find = in.nextInt();
                a.find(find);
                break;
            case 4:
                System.out.println(a.toString());
                break;
            case 5: 
                System.exit(0);
                break;
        }
        System.out.println("Continue? 1-Yes, 2-No");
        int yn = in.nextInt();
        if(yn == 1){
            go();
        }else{
            System.exit(0);
        }
}
@Override
public String toString(){   
    String string = "";
    for(int i = 0; i < location; i++){
        string = string + " " + a[i];
    }
    return "Numbers " + string;
}
}

继承我的主要课程(司机)

public class ArrayManipulator<T> {

   private static int size;
 public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    array a = new array();
    System.out.println("Enter the size of the array");
    size = in.nextInt();
    System.out.println("---------------------------");
    a = new array(size);
    a.go();  
 }
}

所以,我的问题是要求用户在go()方法中输入数组中元素的值。我不是那种用于读取泛型类型的输入读取器,或者甚至是可能的。谢谢

3 个答案:

答案 0 :(得分:1)

在创建类数组的对象之前,最好决定输入什么类型。可能你可以从用户那里得到它。在决定类型后,使用例如:array<Integer,String> arr = new array<Integer,String>创建对象。或者,对于具有泛型类型,请使用Object

答案 1 :(得分:0)

我可以看到您new Scanner(System.in)表示您指的是文字用户输入。这里的问题是你的程序如何知道它应该将文本输入转换成哪种类型?

没有“读取泛型类型”之类的东西,你必须将输入转换为正确的类型(整数,字符串,自定义类等)。

此处的一种方法是向用户询问其他信息,以便区分他/她提供的输入类型。

答案 2 :(得分:0)

就像有些人说的那样,您可以使用Object作为泛型变量类型,尤其是在使用泛型方法或不知道用户会使用哪种数据类型的情况下,就像这样简单的一种:

import java.util.Scanner;

public class GenericMethod {
    public static void main(String[] args) {
        System.out.println("Type something that's yours: ");

        Scanner sc = new Scanner(System.in);

        Object thing;
        thing = sc.next();
        isMine(thing);
    }

    // Generic Method
    public static <T> void isMine(T x) {
        System.out.println(x + " is mine.");
    }
}