【问题标题】:Turn arraylist into array将arraylist变成数组
【发布时间】:2012-02-23 20:30:28
【问题描述】:

这是我目前所拥有的

try{
          // Open the file that is the first 
          // command line parameter
          FileInputStream fstream = new FileInputStream("Patient.txt");
          // Get the object of DataInputStream
          DataInputStream in = new DataInputStream(fstream);
          BufferedReader br = new BufferedReader(new InputStreamReader(in));
          String strLine;
          //Read File Line By Line
          List<String> lines = new ArrayList<String>();
          while ((strLine = br.readLine()) != null) {

          }
          //Close the input stream
          in.close();
            }catch (Exception e){//Catch exception if any
          System.err.println("Error: " + e.getMessage());
          }
    ArrayList<Patient1> patients=new ArrayList<Patient1>();
    Patient1 p = new Patient1();
    //set value to the patient object
    patients.add(p);
    System.out.println(p);
    // File i/o

这是我的txt文件

001, "John", 17, 100, 65, 110, 110, 110, 109, 111, 114, 113, "Swaying, Nausea"
002, "Sun Min", 18, 101, 70, 113, 113, 110, 119, 111, 114, 113, "None"
003, "Ray Allen", 25, 103, 74, 110, 110, 110, 109, 111, 108, 109, "Difficulty driving"
004, "Michael Jordan", 26, 104, 84, 114, 115, 118, 117, 119, 120, 123, "Loss of        bladder control"
005, "Kobe Bryant", 28, 110, 80, 118, 119, 120, 119, 120, 120, 120, "None"

我有我所有的方法和资源,我需要做的就是把数组列表变成一个数组,这样我就可以做类似的事情

    Patient1 average[] = {p[0], p[1], p[2], p[3], p[4]};
    int totalage = 0;
    double totalweight = 0;
    for (int i=0; i<average.length; i++) {
    totalage = (average[i].getAge() + totalage);
    totalweight = (average[i].getWeight() + totalweight);

【问题讨论】:

  • 您是否在重复自己的问题? [如何从文本文件中选择一行并将其转换为数组对象?][1] [1]:stackoverflow.com/questions/9090897/…
  • 你做错了。您应该将列表封装在一个集合中,并使用使用 for-in 语法(或数组中的常规 for 循环)进行迭代和求和的方法。您无需转换为数组。

标签: java arrays methods arraylist


【解决方案1】:

toArray 是你想要的。

例如

ArrayList<String> l = something();
String[] foo = l.toArray(new String[foo.size()]);

ArrayList toArray

编辑

在多看你的例子之后。您无需将其转换为数组,只需在循环中调用 arraylist 上的 get 函数并进行计算。您可能想花一点时间查看 javadoc。

【讨论】:

  • 患者1平均[] = {p[0], p[1], p[2], p[3], p[4]};现在不工作
  • 您的代码示例表明 Patient1 p = new Patient1();这意味着 p 不是一个数组,因此它一开始就不会起作用。
【解决方案2】:

如果您想逐行读取文件,则可以使用 FileUtils 类 common-io

使用FileUtils.readLines()我们可以逐行读取文件内容并将结果作为字符串列表返回

例子:

File file = new File("sample.txt"); 

try { 
List<String> contents = FileUtils.readLines(file); 
for (String line : contents) { 
System.out.println(line);             
} 
} catch (IOException e) { 
e.printStackTrace();     
} 

【讨论】:

【解决方案3】:

这样使用

ArrayList<Patient1> patients = new ArrayList<Patient1>();
Patient1 p = new Patient1();
patients.add(p);
String[] pArray = new String[patients.size()];
pArray = patients.toArray(pArray);
for(String s : pArray)
System.out.println(s);

希望对你有所帮助..

【讨论】:

  • 当你可以调用 api 方法为你做这些时,你为什么要编写所有这些代码?
  • 这行得通,但现在我需要将每一行放入一个数组中,如 p[0] = "";
  • 什么?您正在任意将患者转换为字符串数组?这很奇怪。
猜你喜欢
  • 2016-08-09
  • 2018-06-24
  • 2018-05-16
  • 1970-01-01
  • 2015-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多