猿问

如何将数组的索引打印到单独的 ArrayList 中

我为我的程序创建了一个方法,我在其中比较了两个数组 user 和 test。当它与测试数组不同时,我试图将数组用户的索引添加到 ArrayList qMissed 中。如果两个数组完全相同,那么它应该只返回 null。我收到异常错误,因为我需要完成引用类型,但我不确定该怎么做。


 /**

 * @param user

 * @param test

 * @return

 */

 public static ArrayList<String> questionMissed(String[] user, String[] test) 

 {   

    ArrayList<int> qMissed = new ArrayList<int>();

    for (int i = 0; i <= user.length-1; i++)

    {

       if (user[i] != test[i])

       {

          qMissed = Arrays.asList(qMissed).indexOf(user);(i+1);

       }

       else if (user[i] == test[i])

       {

          return null; 

       }

    }

    return qMissed;

 }


忽然笑
浏览 180回答 3
3回答

慕村225694

您的方法似乎有一些逻辑和编译问题。看起来你需要这个方法,public static List<Integer> questionMissed(String[] user, String[] test) {&nbsp; &nbsp; List<Integer> qMissed = new ArrayList<Integer>();&nbsp; &nbsp; for (int i = 0; i < user.length; i++) {&nbsp; &nbsp; &nbsp; &nbsp; if (!user[i].equals(test[i])) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qMissed.add(i);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; return qMissed.size() == 0 ? null : qMissed;}修复和他们的解释,1. Your return type has to be List<Integer> instead of ArrayList<String> because you want to return an ArrayList of Integer indexes and not string.2. Second problem you can't use primitive type in ArrayList<int> instead you need to use ArrayList<Integer>3. You can't compare strings with == instead you need to use equals method on string.4. You don't have to return null inside forloop else hence else block I have removed.5. After you exit the forloop, as you want to return null if both element's arrays matched hence this code,return qMissed.size() == 0 ? null : qMissed;如果您在使用此方法时遇到任何问题,请告诉我。编辑:如果两个传递的数组具有相同的数字,如何显示“全部正确”消息。你一定是这样称呼它的,List<Integer> list = questionMissed(user,test);if (list == null) {&nbsp; &nbsp; System.out.println("All are correct");} else {&nbsp; &nbsp; // your current code}

牛魔王的故事

您可以尝试在方法中将返回类型从 ArrayList 更改为 ArrayList:public static ArrayList<int> questionMissed(String[] user, String[] test) {&nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; ArrayList<int> qMissed = new ArrayList<int>();&nbsp; &nbsp; &nbsp; &nbsp; for (int i=0;i<=user.length-1;i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (user[i] != test[i]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; qMissed = Arrays.asList(qMissed).indexOf(user);(i+1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp;return qMissed;&nbsp; &nbsp; }如果条件原因是多余的,您也可以删除 else。请附上您得到的例外情况。

慕田峪4524236

我看到您的代码存在多个问题,首先,正如 Andreas 所说 ArrayList 无法承载原始类型,因此将其更改为ArrayList<Integer> qMissed = new ArrayList<Integer>();我看到的第二个问题是,您使用==此比较比较字符串可能是错误的,因此请equals改用(user[i].equals(test[i]))我看到的最后一个错误是代码无法编译,你能否在评论中给我更多信息,说明你在这部分尝试做的事情,因为它不是有效的代码qMissed = Arrays.asList(qMissed).indexOf(user);(i + 1);如果你想做类似 Pushpesh Kumar Rajwanshi 回答的事情,你可以使用 java 8 流,它的作用是创建一个具有用户长度的 IntStream,然后过滤它以仅包含在相同索引处不相等的项目,然后将其添加到qMissed.public static List<Integer> questionMissed(String[] user, String[] test) {&nbsp; &nbsp; List<Integer> qMissed = new ArrayList<>();&nbsp; &nbsp; &nbsp;IntStream.range(0, user.length)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .filter(i -> !user[i].equals(test[i]))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .forEach(qMissed::add);&nbsp; &nbsp; return qMissed.size() == 0 ? null : qMissed;}
随时随地看视频慕课网APP

相关分类

Java
我要回答