Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/334.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
for元素在java中包含null_Java_For Loop_Null - Fatal编程技术网

for元素在java中包含null

for元素在java中包含null,java,for-loop,null,Java,For Loop,Null,这里有一些我想做的sudo代码 String[] listA = new String { "a1", "a2"} String[] listB = new String { null} String[] listC = new String { "c1", "c2"} for( String a : listA) { for( String b : listB) { for( String c : listC) { if( a!=null) System.out.p

这里有一些我想做的sudo代码

String[] listA = new String { "a1", "a2"}
String[] listB = new String { null}
String[] listC = new String { "c1", "c2"}

for( String a : listA) {
  for( String b : listB) {
    for( String c : listC) {
       if( a!=null) System.out.print( a);
       System.out.print(",");
       if( b!=null) System.out.print(b);
       System.out.print(",");
       if( c!=null) System.out.print(c);
       System.out.println("");
    }
  }
}
我的预期结果是

a1,,c1
a1,,c2
a2,,c1
a2,,c2
但由于listB为空,所以无法打印代码逻辑。我试着检查列表,并做出如下逻辑的所有可能性

if( listA != null) {
  for( String a : list A) {
     if( listB !=null) {
        for(String b : listB) {
           if( listC != null) {
           }
           else { 
                ...
           }
        }
     }
     else {
          ....
     }
  }
}
else {
    ...... similar code in here
}
我认为这不是解决这个问题的最好办法。你知道吗?

这在java中是错误的

String[] listA = new String { "a1", "a2"}
String[] listB = new String { null}
String[] listC = new String { "c1", "c2"}
试试这个。这对你有用

public class prac {
    public static void main(String[] args) {
        String[] listA = {"a1","a2"};
        String[] listB = {null};
        String[] listC = {"c1", "c2"};

for( String a : listA) {
  for( String b : listB) {
    for( String c : listC) {
       if( a!=null) System.out.print( a);
       System.out.print(",");
       if( b!=null) System.out.print(b);
       System.out.print(",");
       if( c!=null) System.out.print(c);
       System.out.println("");
    }
  }
}

    }

}
这在java中是错误的

String[] listA = new String { "a1", "a2"}
String[] listB = new String { null}
String[] listC = new String { "c1", "c2"}
试试这个。这对你有用

public class prac {
    public static void main(String[] args) {
        String[] listA = {"a1","a2"};
        String[] listB = {null};
        String[] listC = {"c1", "c2"};

for( String a : listA) {
  for( String b : listB) {
    for( String c : listC) {
       if( a!=null) System.out.print( a);
       System.out.print(",");
       if( b!=null) System.out.print(b);
       System.out.print(",");
       if( c!=null) System.out.print(c);
       System.out.println("");
    }
  }
}

    }

}

您需要的是三个集合(列表)的集合。但是,当其中一个列表为空时,它将被一个包含“空元素”的集合替换

// pseudo code
String[] safeList(String[] list) { if list.length == 0 return {''} else return list; }

// carthesian product with a twist
static void safeProductWithATwist(
    String[] listA, String[] listB, String[] listC) {
    for(String a: safeList(listA)) 
      for(String b: safeList(listB)) 
        for(String c: safeList(listC)) 
          foo(a, b, c);
}

您需要的是三个集合(列表)的集合。但是,当其中一个列表为空时,它将被一个包含“空元素”的集合替换

// pseudo code
String[] safeList(String[] list) { if list.length == 0 return {''} else return list; }

// carthesian product with a twist
static void safeProductWithATwist(
    String[] listA, String[] listB, String[] listC) {
    for(String a: safeList(listA)) 
      for(String b: safeList(listB)) 
        for(String c: safeList(listC)) 
          foo(a, b, c);
}
输出

Count1


a1,,a1
a1,,a1
Count2


a2,,a2
a2,,a2
它将进入循环,因为
null
列表b中的一个元素。 所以它不会打印b,因为您正在检查
b=空
。 在回答中,代码中的一些错误也被纠正了

输出

Count1


a1,,a1
a1,,a1
Count2


a2,,a2
a2,,a2
它将进入循环,因为
null
列表b中的一个元素。 所以它不会打印b,因为您正在检查
b=空

在回答中,代码中的一些错误也得到了纠正。

也许你应该在问题中加入一些真正的Java代码。现在,你的程序不工作了,因为
print a
print“,“
无效。将打印代码更改为真正的Java代码。那么你现在拥有的应该可以工作了。问题是什么?一旦您通过添加[](例如
String[]listA=newstring[]{“a1”,“a2”};
)解决了数组声明中的编译问题,您的代码就可以正常工作。也许您应该在问题中加入一些真正的Java代码。现在,您的程序无法工作,因为
print a
print”
无效。已将打印代码更改为真实的java代码。那么,您现在拥有的应该可以使用了。问题是什么?一旦您通过添加[](例如
String[]listA=newstring[]{“a1”,“a2”};
)修复了数组声明中的编译问题,您的代码就可以正常工作了。