【问题标题】:insert values into integer array from List of type string从字符串类型的列表中将值插入整数数组
【发布时间】:2016-05-19 17:41:24
【问题描述】:

我希望将第 5 行中打印的值插入到整数数组中。 该文件同时包含整数值和字符串值
****我还在学习中**** 对不起,我稍微改变了问题。 谢谢

     File f = new File("SampleInput.txt");
          try{
               ArrayList<String> lines = get_arraylist_from_file(f);       
               for(int x =23; x < lines.size(); x++){
                   System.out.println(lines.get(x));
                   **enter code here**
               }
            }
    catch(Exception e){
        System.out.println("File not found!!!!");
    }
}
public static ArrayList<String> get_arraylist_from_file(File f) 
    throws FileNotFoundException {
    Scanner s;
    ArrayList<String> list = new ArrayList<String>();
    s = new Scanner(f);
    while (s.hasNext()) {
        list.add(s.next());
    }
    s.close();
    return list;
}

【问题讨论】:

  • 为什么不使用列表而不是数组?

标签: java arrays list file-handling


【解决方案1】:

如下更容易使用整数数组列表

List<Integer> list = new ArrayList<Integer>();
File f = new File("SampleInput.txt");
          try{
               ArrayList<String> lines = get_arraylist_from_file(f);       
               for(int x =23; x < lines.size(); x++){
                   System.out.println(lines.get(x));
                   list.add(Integer.parseInt(lines.get(x)));
               }
            }
    catch(Exception e){
        System.out.println("File not found!!!!");
    }
}

【讨论】:

    【解决方案2】:
          List<Integer> numList = new ArrayList<>();
          File f = new File("SampleInput.txt");
          try{
               ArrayList<String> lines = get_arraylist_from_file(f);       
               for(int x =23; x < lines.size(); x++){
                   System.out.println(lines.get(x));
                   **enter code here**
                   numList.add(Integer.parseInt(lines.get(x))); 
               }
            }
    

    【讨论】:

      【解决方案3】:

      你必须在循环外创建一个适当大小的 int 数组,然后只需解析字符串并将它们添加到循环中的数组中:

                 ArrayList<String> lines = get_arraylist_from_file(f); 
                 int[] intArray = new int[lines.size-23];
                 for(int x =23; x < lines.size(); x++){
                     System.out.println(lines.get(x));
                     //**enter code here**
                     String line = lines.get(x);
                     intArray[x-23] = Integer.parseInt(line);
                 }
      

      【讨论】:

      • 你也可以只做= Integer.parseInt(lines.get(x));
      【解决方案4】:

      我猜你想要这样的东西,

      try{
        ArrayList<String> lines = get_arraylist_from_file(f);
        ArrayList<int> intLines = new ArrayList();
        for (int x = 23; x < lines.size(); x++) {
          System.out.println(lines.get(x));
          intLines.add(Integer.parseInt(lines.get(x)));
        }
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-08-02
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多