Java 如何从列表构建数组?(不含ArrayList)

Java 如何从列表构建数组?(不含ArrayList),java,arrays,list,file,Java,Arrays,List,File,我正试图从一个文件中构建一个数组,该文件列出了43个国家,并在各自的行中列出了其他4个关于这些国家的特定信息字段。我得到了代码,我必须阅读代码(通过echo打印验证),但我不确定如何处理其余的代码,以便在输出方面获得一些结果 public static void main(String[] args) throws IOException { // TODO code application logic here String inputString = null;

我正试图从一个文件中构建一个数组,该文件列出了43个国家,并在各自的行中列出了其他4个关于这些国家的特定信息字段。我得到了代码,我必须阅读代码(通过echo打印验证),但我不确定如何处理其余的代码,以便在输出方面获得一些结果

 public static void main(String[] args) throws IOException {
    // TODO code application logic here

        String inputString = null; 
        String cName;
        String cCapital;
        String regionNum;
        int capitalPop;
        int region;

        File f = new File("Countries.txt"); 
        FileReader fr = new FileReader(f); 
        BufferedReader br = new BufferedReader(fr); 


        while (inputString != null)
        { 
            cName = inputString.substring(0, 15).trim();
            cCapital = inputString.substring(15, 30); 
            capitalPop = Integer.parseInt(inputString.substring(55, 60));

            inputString = br.readLine(); 

        }

        br.close(); 
        fr.close(); 
}

while循环中的三行具有用于从文件行修剪的采样值。我想我的主要问题是我在概念上感到困惑。欢迎您的任何意见。谢谢你

我在你的代码中没有看到
列表
对象。。。但要获取列表并将其放入数组,请调用
toArray()
方法,请参阅api文档

我们中没有人在您的代码中看到
列表,但应该这样做:

您需要创建一个ArrayList,然后向其中添加每个国家/地区的对象。如果国家数始终固定为43,则可以直接使用数组

public static void main(String[] args) throws IOException {
    // TODO code application logic here

        String inputString = null; 
        String cName;
        String cCapital;
        String regionNum;
        int capitalPop;
        int region;

        File f = new File("Countries.txt"); 
        FileReader fr = new FileReader(f); 
        BufferedReader br = new BufferedReader(fr); 
        List<Country> countries = new ArrayList<Country>();
        inputString = br.readLine();    
        while (inputString != null)
        { 
            cName = inputString.substring(0, 15).trim();
            cCapital = inputString.substring(15, 30); 
            capitalPop = Integer.parseInt(inputString.substring(55, 60));
            Country country = new Country();
            country.setCName(cName);
            // set other members
            countries.add(country); // This is your list of countries
            inputString = br.readLine();    
        }
        Country[] countryArray = new Country[countries.size()];
        countries.toArray(countryArray);
        // voila.. the country array.
        br.close(); 
        fr.close(); 
}


public class Country {

 private String cName;
 private String cCapital;
 //.. other fields;


 public String getCName(){ return this.cName;}
 public void setCName(String cName) {this.cName = cName;}
 // create other setters / getters for each of the member variables.
}
publicstaticvoidmain(字符串[]args)引发IOException{
//此处的TODO代码应用程序逻辑
字符串inputString=null;
字符串cName;
弦柱头;
字符串regionNum;
首都国际酒店;
int区域;
文件f=新文件(“Countries.txt”);
FileReader fr=新的FileReader(f);
BufferedReader br=新的BufferedReader(fr);
列表国家=新的ArrayList();
inputString=br.readLine();
while(inputString!=null)
{ 
cName=inputString.substring(0,15).trim();
cCapital=inputString.substring(15,30);
capitalPop=Integer.parseInt(inputString.substring(55,60));
国家=新国家();
country.setCName(cName);
//设置其他成员
国家。添加(国家);//这是您的国家列表
inputString=br.readLine();
}
Country[]countryArray=新国家/地区[countries.size()];
国家。toArray(countryArray);
//瞧,乡村阵列。
br.close();
fr.close();
}
公营国家{
私有字符串cName;
私人资本;
//……其他领域;
公共字符串getCName(){返回this.cName;}
public void setCName(字符串cName){this.cName=cName;}
//为每个成员变量创建其他setter/getter。
}

我会阅读CSV文件(逗号分隔值或逗号分隔值)以及从中读取的常用方法。诸如Scanner之类的类可以用特定的值分隔每个读取行,因此您可以依赖该分隔符,而不是显式地声明字符长度。根据文本在文件中的组织方式,这可能更有效、更合理。此外,分隔符可以是任何字符串,最常见的是“,”


此外,我还将定义一个类,该类保存存储所需的值,并创建一个保存数据的数组。然后,您可以稍后读回该数组,并调用您为其定义的toString()方法来打印存储值。

这是计算机科学类的吗?我在这里没有看到列表。是的,仔细检查了。。未找到列表。与问题无关,但您需要放置一行
inputString=br.readLine() > while循环>代码>(输入字符串!= null)< /代码>。您的问题可以重新措辞,因为您不从我们考虑的实际“列表”开始,而只是一个“代码>字符串,它的项被空间字符分隔开。