如何在for-each循环中获取备用值?

时间:2018-09-12 04:48:10

标签: java

如何在for-each循环中获取备用值?

String [] yesBank = {"bdshfdhbck", "gsdhkcvdcjh", "fbdsjkfdsfj", "njjhsdfbjsfggsj"};
for(String arrayOFWord : yesBank)
{
    System.out.println(arrayOFWord);
}

2 个答案:

答案 0 :(得分:0)

您可以使用一些count变量,因为替代值在偶数或奇数索引中。

尝试此操作,甚至对于idexe

String [] yesBank = {"bdshfdhbck", "gsdhkcvdcjh", "fbdsjkfdsfj", "njjhsdfbjsfggsj"};
int count=0;
for(String arrayOFWord : yesBank)
{
    if((count++)%2 == 0)
        System.out.println(arrayOFWord);    
}

答案 1 :(得分:0)

您可以尝试使用偶数索引:

String [] yesBank = {"bdshfdhbck", "gsdhkcvdcjh", "fbdsjkfdsfj", "njjhsdfbjsfggsj"};
        for(String arrayOFWord : yesBank)
        {
        if (Arrays.asList(yesBank).indexOf(arrayOFWord)%2  ==  0) System.out.println(arrayOFWord);    
        }