Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/333.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 对于每个对象,在数组中查找对象并返回该对象_Java_Arraylist_Foreach - Fatal编程技术网

Java 对于每个对象,在数组中查找对象并返回该对象

Java 对于每个对象,在数组中查找对象并返回该对象,java,arraylist,foreach,Java,Arraylist,Foreach,新来的,有个小问题让我头疼。我觉得这就像是一行需要修复的代码,但就我的一生而言,我无法理解它 我应该有一个for-each循环,它在数组中定位一个对象,如果该对象存在,则返回该对象,如果该对象不存在,则返回null。但对我来说有一件奇怪的事,它可以找到一个很好的对象,它会将它设置为某个东西,而不是对象,只是某个东西。我可以说,因为其他方法依赖于这项工作,这项工作只是没有返回对象的任何原因。不管怎样,这是密码,我想你不需要任何其他东西 public Icosahedron findIcosa

新来的,有个小问题让我头疼。我觉得这就像是一行需要修复的代码,但就我的一生而言,我无法理解它

我应该有一个for-each循环,它在数组中定位一个对象,如果该对象存在,则返回该对象,如果该对象不存在,则返回null。但对我来说有一件奇怪的事,它可以找到一个很好的对象,它会将它设置为某个东西,而不是对象,只是某个东西。我可以说,因为其他方法依赖于这项工作,这项工作只是没有返回对象的任何原因。不管怎样,这是密码,我想你不需要任何其他东西

   public Icosahedron findIcosahedron(String labelIn) {
      Icosahedron output;
      output = null;
      for (Icosahedron i : iList) {
         if (i.getLabel().equalsIgnoreCase(labelIn))  {
            output = i;
         }
      }
      return output;
   }
请求:

 case 'F':
           System.out.print("\tLabel: ");
           label = userInput.nextLine();
           if (myIcosahedronList.findIcosahedron(label) != null) {
               myIcosahedronList.findIcosahedron(label);
           }
           else {
              System.out.println("\"" + label + "\" not found");
           }
           break; 

找到对象后,应停止进一步搜索:

  for (Icosahedron i : iList) {
     if (i.getLabel().equalsIgnoreCase(labelIn))  {
        output = i;
        break;
     }
  }
使用
break
退出循环

在代码库的第二部分:

case 'F':
           System.out.print("\tLabel: ");
           label = userInput.nextLine();
           Icosahedron icosahedron = myIcosahedronList.findIcosahedron(label);
           if ( icosahedron == null) {
               System.out.println("\"" + label + "\" not found");
           }
           else {
              // Do something with icosahedron
           }
           break; 

您的代码不包含System.out.print()函数,用于在找到对象时运行该函数,而不对其返回的内容执行任何操作:

 case 'F':
           System.out.print("\tLabel: ");
           label = userInput.nextLine();
           if (myIcosahedronList.findIcosahedron(label) != null) {
               myIcosahedronList.findIcosahedron(label); // <- here
           }
           else {
              System.out.println("\"" + label + "\" not found");
           }
           break; 
案例“F”:
系统输出打印(“\t标签:”);
label=userInput.nextLine();
if(my二十面体列表.findIcosahedron(标签)!=null){

My二十面体列表。findIcosahedron(标签);//你怎么知道它没有返回对象?@Nupadhayaya我有另一组代码,当你输入标签时,它会说一些类似“它存在”的东西,然后返回对象。相反,它只是说对象存在,然后转到循环中的下一位。你能发布代码吗?你没有分配myIcosa的输出吗面体列表。findIcosahedron(标签)我同意这一点,但为了进行最低限度的更改并帮助OP了解原因,我改变了这种方式。尝试了这种方式,但似乎没有什么效果。不能只为这个赋值返回i,因为如果它不存在,我需要返回null,除非我愿意ld null,直到它被分配给其他对象?