java - 如何重新转换java数组

标签 java arrays

所以我试图读取一些用户输入,然后根据两个字符串数组进行检查,然后如果用户输入等于两个数组中的某个值,我想将用户输入放入一个新数组中,但是当我去打印应包含用户输入的新数组时,它会打印数组中的每个值都为空

    String[] VegiFruit = {"Apples", "Lettuce", "Broccoli"};
    String[] Meats = {"Ground beef", "Hambuger"};
    String[] Input = new String[20];
    String[] InputGreen = new String[20];
    String done = "done";
    Scanner USER_IN = new Scanner(System.in);
    Methods Use = new Methods();    //This is another class I have that just makes printing blank lines and borders look nicer

    Use.border();
    System.out.println("Enter Item name then follow instructions.");
    Use.space();

    for(int i = 0; i < 21; i++)
    {
        System.out.print(i+": ");
        Input[i] = USER_IN.nextLine();
        if(Arrays.asList(VegiFruit).contains(Input))
        {
            InputGreen[i] = Input[i];
            System.out.println(InputGreen[i]); //Prints null for every value
        }
    }

那么我的逻辑有问题吗?还是别的什么?

最佳答案

if(Arrays.asList(VegiFruit).contains(Input))

这一行是完全错误的,因为您正在将对象(这里输入是 .contains 方法的参数值,它是一个对象)与元素列表进行比较,这里列表的元素是一个字符串值。

因此更改代码如下,

if (Arrays.asList(VegiFruit).contains(Input[i])) {
    InputGreen[i] = Input[i];
    System.out.println(InputGreen[i]); //Now it is print the corresponding value.
}

关于java - 如何重新转换java数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32571884/

相关文章:

javascript - Heroku 环境导致 Java/Nashorn/ReactJS 应用程序崩溃

java - Antlr4 "Test Rig"和行家

javascript - array.splice 在 Angular Js 指令中显示奇怪的行为

c - fork() with char array and qsort() resulting child 停止工作

java - 当使用\n 从字符串中读取 double 值时,扫描仪会出现 InputMismatchException — Java

java - 如何从 JTable 中检索 header 值

java - Hibernate.initialize() 似乎不适用于延迟加载的集合

c++ - 如何从 getter 函数中的类返回结构数组

递归打印方法中的数组

java - 将 double 转换为 byte[] 数组