Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将arraylist转换为array_Java_Arrays_Methods_Arraylist - Fatal编程技术网

Java 将arraylist转换为array

Java 将arraylist转换为array,java,arrays,methods,arraylist,Java,Arrays,Methods,Arraylist,这就是我目前所拥有的 try{ // Open the file that is the first // command line parameter FileInputStream fstream = new FileInputStream("Patient.txt"); // Get the object of DataInputStream DataInputStream in = ne

这就是我目前所拥有的

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
我有我所有的方法和资源,我需要做的就是把数组列表变成一个数组,这样我就可以像

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

Patient1 average[]={p[0],p[1],p[2],p[3],p[4]};
整数总年龄=0;
双倍总重量=0;

for(inti=0;i
toArray
是您想要的

e、 g

ArrayList l=something();
字符串[]foo=l.toArray(新字符串[foo.size()]);

编辑

再看一下您的示例。您不需要将其转换为数组,只需在循环中的arraylist上调用get函数并进行计算。您可能需要花一点时间查看javadoc。

这样使用

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);
ArrayList患者=新建ArrayList();
Patient1 p=新的Patient1();
加(p);
String[]pArray=新字符串[patients.size()];
帕雷=患者。托雷(帕雷);
用于(字符串s:pArray)
系统输出打印项次;

希望这将对您有所帮助。

如果您想逐行读取文件,那么您可以使用
公共io的FileUtils类

使用
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();     
} 
File File=new文件(“sample.txt”);
试试{
列表内容=FileUtils.readLines(文件);
对于(字符串行:内容){
系统输出打印项次(行);
} 
}捕获(IOE){
e、 printStackTrace();
} 

Patient1 average[]={p[0],p[1],p[2],p[3],p[4]};现在不起作用您的代码示例说Patient1 p=new Patient1();这意味着p不是一个数组,所以一开始就不起作用。您是否重复了自己的问题?[如何从文本文件中选取一行并将其转换为数组对象?][1][1]:您做错了。您应该将列表封装在集合中,并使用for in语法(或数组中的常规for循环)的方法进行迭代和求和。您不需要转换为数组。当您可以调用api方法来为您执行此操作时,为什么要编写所有代码?这是可行的,但现在我需要将每一行放入一个数组中,如p[0]=“”;什么?你任意将患者转换为字符串数组?这很奇怪。请参考您将了解的API org.apache.commons.io.FileUtils…或者您可以使用内置的Scanner类。这在5之后的所有Java中都可用。
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);
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();     
}