Java displaying array one by one by using indexes

前端 未结 3 1227
遇见更好的自我
遇见更好的自我 2021-01-26 18:16

I am having trouble displaying an array by index, I don\'t know why this is happening. Any help will be greatly appreciated. Here is a snippet of my code:

// cre         


        
相关标签:
3条回答
  • 2021-01-26 19:02

    improve your for loop:

    // for-each loop
    for(int i=0;i<tempsArray2.length;i++){
        // display ss
        System.out.println(tempsArray2[i]);
    }
    

    If you prefer for-each:

    // for-each loop
    for(String ss : tempsArray2){
    
        // display ss
        System.out.println(ss);
    }
    
    0 讨论(0)
  • 2021-01-26 19:03
    // for-each loop
    for(String ss : tempsArray2){
    
        // display ss
        System.out.println(tempsArray2[0]);
    

    your problem is here. you're not actually using the ss variable at all, you're simply displaying the first string each time around the loop.

    0 讨论(0)
  • 2021-01-26 19:05

    You have put in your enhanced for loop correctly, its only the items you are not picking properly. Using enhanced for loop loop allows you to pick items without using indexes.

    Change your loop from

    // for-each loop
        for(String ss : tempsArray2){
    
            // display ss
            System.out.println(tempsArray2[0]);
        }
    

    to

    // for-each loop
        for(String ss : tempsArray2){
    
            // display ss
            System.out.println(ss);
        }
    
    0 讨论(0)
提交回复
热议问题