Java的一个显示阵列一个接使用索引 [英] Java displaying array one by one by using indexes

查看:121
本文介绍了Java的一个显示阵列一个接使用索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有显示通过索引一个数组的麻烦,我不知道为什么会这样。任何帮助将大大AP preciated。这里是我的code的一个片段:

  //创建token2
    串token2 =;    //创建扫描仪inFile2
    扫描仪inFile2 =新的扫描仪(新文件
    (/Users/timothylee/KeyWestHumid.txt))。
            useDelimiter(\\\\ S *);    //创建temps2
    清单<串GT; temps2 =新的LinkedList<串GT;();    // while循环
    而(inFile2.hasNext()){        // 找下一个
        token2 = inFile2.next();        //初始化temps2
        temps2.add(token2);
    }    //关闭inFile2
    inFile2.close();    //创建数组
    的String [] = tempsArray2 temps2.toArray(新的String [0]);    // for-each循环
    对于(字符串SS:tempsArray2){        //显示SS
        的System.out.println(tempsArray2 [0]);
    }


解决方案

改善您的循环:

  // for-each循环
的for(int i = 0; I< tempsArray2.length;我++){
    //显示SS
    的System.out.println(tempsArray2 [I]);
}

如果您preFER 的for-each

  // for-each循环
对于(字符串SS:tempsArray2){    //显示SS
    的System.out.println(SS);
}

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:

// create token2
    String token2 = "";

    // create Scanner inFile2
    Scanner inFile2 = new Scanner(new File
    ("/Users/timothylee/KeyWestHumid.txt")).
            useDelimiter(",\\s*");

    // create temps2
    List<String> temps2 = new LinkedList<String>();

    // while loop
    while(inFile2.hasNext()){

        // find next
        token2 = inFile2.next();

        // initialize temps2
        temps2.add(token2);
    }

    // close inFile2
    inFile2.close();

    // create array
    String[] tempsArray2 = temps2.toArray(new String[0]);

    // for-each loop
    for(String ss : tempsArray2){

        // display ss
        System.out.println(tempsArray2[0]);
    }

解决方案

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);
}

这篇关于Java的一个显示阵列一个接使用索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆