Java 使用对象的数组或arrayList,以便文件中的所有记录都可以存储在内存中

Java 使用对象的数组或arrayList,以便文件中的所有记录都可以存储在内存中,java,arrays,csv,oop,arraylist,Java,Arrays,Csv,Oop,Arraylist,我是Java初学者,我的练习有问题 这是我的Client.txt文件: 1,周杰伦,沃克,新南威尔士州巴格特维尔博兰大道91号,邮编2477 新南威尔士州MILLERS POINT海洋大道45号洛韦梅尔2号 2016年,新南威尔士州雷德芬Edgecliff路32号海牛休3号 新南威尔士州哈顿山韦伯路93号特纳伊丽莎白4号,邮编2290 这是我的客户端类(具有构造函数): 公共类客户端{ 私人int客户ID; 私有字符串名; 私家姓; 私家弦街;; 私人住宅; 私有字符串状态; 私人邮政编码;

我是Java初学者,我的练习有问题

这是我的Client.txt文件:

1,周杰伦,沃克,新南威尔士州巴格特维尔博兰大道91号,邮编2477
新南威尔士州MILLERS POINT海洋大道45号洛韦梅尔2号
2016年,新南威尔士州雷德芬Edgecliff路32号海牛休3号
新南威尔士州哈顿山韦伯路93号特纳伊丽莎白4号,邮编2290
这是我的客户端类(具有构造函数):

公共类客户端{
私人int客户ID;
私有字符串名;
私家姓;
私家弦街;;
私人住宅;
私有字符串状态;
私人邮政编码;
//建造师
公共客户端(int-ID、String-fName、String-sName、String-str、String-sb、String-sta、int-pCode){
clientID=ID;
firstName=fName;
姓氏=斯奈姆;
街道=str;
郊区=某人;
状态=sta;
邮政编码=邮政编码;
}
这是我的代码创建对象并读取从txt文件读取的记录:

File infle=新文件(“clients.txt”);
扫描仪输入文件=新扫描仪(填充);
字符串str;
字符串[]标记;
while(inputFile.hasNext()){
str=inputFile.nextLine();//从文件中读取一行文本
tokens=str.split(,“”;//使用逗号作为分隔符拆分行
//将已打印的每个标记映射到客户端类中的相应字段
//因为令牌[0]的类型为String,而clientID的类型为int,
//我们需要解析它并得到整数表示。
//我们也对邮政编码做同样的事情
int clientID=Integer.parseInt(标记[0]);
String firstName=tokens[1];
字符串姓氏=标记[2];
String street=代币[3];
字符串=令牌[4];
字符串状态=令牌[5];
int postcode=Integer.parseInt(标记[6]);
//创建“客户端”类型的新对象
//并传递所有收集到的信息。
客户=新客户(客户ID、名字、姓氏、街道、郊区、州、邮政编码);
System.out.println(客户端+“\n”);
}//结束时
练习的要求是现在我必须修改程序以使用此客户端对象的数组或arrayList,以便文件中的所有客户端记录都可以轻松存储在内存中,并且每个对象都是从文件创建的,将其放入客户端对象的数组/arrayList中


我刚刚开始学习array和arrayList几个星期,所以我不想做这个练习。有人能帮我吗?

在你的
之前,而
循环创建
arrayList

ArrayList<Client> cList = new ArrayList<> ();

在while循环上方,您可以简单地创建一个新列表

List<Client> clientList = new ArrayList<>();
List clientList=new ArrayList();
然后在创建每个
新客户机(…)


使用
clientList.add(client)
可以覆盖
client
的构造函数,这样它也可以从文件中获取一行内容:

import java.util.stream.Stream;

public class Client {
    private int clientId;
    private String firstName;
    private String lastName;
    private String street;
    private String suburb;
    private String state;
    private int postcode;

    public Client(int clientId, String firstName, String lastName, String street, String suburb, String state,
            int postcode) {
        this.clientId = clientId;
        this.firstName = firstName;
        this.lastName = lastName;
        this.street = street;
        this.suburb = suburb;
        this.state = state;
        this.postcode = postcode;
    }

    public Client(String line) {
        String[] split = line.split(",");
        this.clientId = Integer.parseInt(split[0]);
        this.firstName = split[1];
        this.lastName = split[2].trim();
        this.street = split[3];
        this.suburb = titleCase(split[4]);
        this.state = split[5];
        this.postcode = Integer.parseInt(split[6]);
    }

    @Override
    public String toString() {
        return String.format("%s %s - %s, %s - %s, %d", this.firstName, this.lastName, this.street, this.suburb,
                this.state, this.postcode);
    }

    private String titleCase(String suburb) {
        return Stream.of(suburb.split(" ")).map(w -> w.toUpperCase().charAt(0) + w.toLowerCase().substring(1))
                .reduce((s, s2) -> s + " " + s2).orElse("");
    }
}
也可用于以下用途:


试试看。

你需要问一个实际问题。请参阅:
import java.util.stream.Stream;

public class Client {
    private int clientId;
    private String firstName;
    private String lastName;
    private String street;
    private String suburb;
    private String state;
    private int postcode;

    public Client(int clientId, String firstName, String lastName, String street, String suburb, String state,
            int postcode) {
        this.clientId = clientId;
        this.firstName = firstName;
        this.lastName = lastName;
        this.street = street;
        this.suburb = suburb;
        this.state = state;
        this.postcode = postcode;
    }

    public Client(String line) {
        String[] split = line.split(",");
        this.clientId = Integer.parseInt(split[0]);
        this.firstName = split[1];
        this.lastName = split[2].trim();
        this.street = split[3];
        this.suburb = titleCase(split[4]);
        this.state = split[5];
        this.postcode = Integer.parseInt(split[6]);
    }

    @Override
    public String toString() {
        return String.format("%s %s - %s, %s - %s, %d", this.firstName, this.lastName, this.street, this.suburb,
                this.state, this.postcode);
    }

    private String titleCase(String suburb) {
        return Stream.of(suburb.split(" ")).map(w -> w.toUpperCase().charAt(0) + w.toLowerCase().substring(1))
                .reduce((s, s2) -> s + " " + s2).orElse("");
    }
}
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

class Main {
    public static void main(String[] args) {
        List<Client> clientList = new ArrayList<>();

        try (Scanner scanner = new Scanner(new File("Clients.txt"))) {
            while (scanner.hasNext()) {
                String line = scanner.nextLine();
                clientList.add(new Client(line));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        for (Client client : clientList) {
            System.out.println(client);
        }
    }
}
Jay Walker - 91 Boland Drive, Bagotville - NSW, 2477
Mel Lowe - 45 Ocean Drive, Millers Point - NSW, 2000
Hugh Manatee - 32 Edgecliff Road, Redfern - NSW, 2016
Elizabeth Turner - 93 Webb Road, Mount Hutton - NSW, 2290