使用对象的array或arrayList,以便可以将其来自文件的所有记录存储在内存中 [英] use an array or arrayList ofan objects so that all of its records from the file can be stored in memory

查看:38
本文介绍了使用对象的array或arrayList,以便可以将其来自文件的所有记录存储在内存中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java初学者,我在练习时遇到了麻烦.

I'm a Java beginner and I'm having trouble with my exercises.

这是我的Client.txt文件:

This is my Client.txt file:

1,Jay, Walker,91 Boland Drive,BAGOTVILLE,NSW,2477
2,Mel, Lowe,45 Ocean Drive,MILLERS POINT,NSW,2000
3,Hugh, Manatee,32 Edgecliff Road,REDFERN,NSW,2016
4,Elizabeth, Turner,93 Webb Road,MOUNT HUTTON,NSW,2290

这是我的Client类(具有构造函数):

This is my Client class (have constructor):

public class Client {
    private int clientID;
    private String firstName;
    private String surName;
    private String street;
    private String suburb;
    private String state;
    private int postcode;
    
    // constructor
    public Client (int ID, String fName, String sName, String str, String sb, String sta, int pCode) {
        
        clientID = ID;
        firstName = fName;
        surName = sName;
        street = str;
        suburb = sb;
        state = sta;
        postcode = pCode;
    }

这是我的代码创建对象,并读取从txt文件读取的记录:

This is my code creating object and read the records that are read from the txt file:

File inFile = new File("clients.txt");
Scanner inputFile = new Scanner(inFile);
String str;
String[] tokens;
while (inputFile.hasNext()) {
    
    str = inputFile.nextLine();         // read a line of text from the file 
    tokens = str.split(",");            // split the line using commas as delimiter
    
    // map each token that is already printed to corresponding field in Client class
    // Because tokens[0] is of type String but clientID is of type int,
    // we need to parse it and get the integer representation.
    // we also do the same thing with postcode 
    int clientID = Integer.parseInt(tokens[0]);
    String firstName = tokens[1];
    String surName = tokens[2];
    String street = tokens[3];
    String suburb = tokens[4];
    String state = tokens[5];
    int postcode = Integer.parseInt(tokens[6]); 
    
    // create a new object of `Client` type
    // and pass all the gathered information.
    Client client = new Client(clientID, firstName, surName, street, suburb, state, postcode);
    System.out.println(client + "\n");
        
} // end while

现在练习的要求是我必须修改程序以利用此Client对象的array或arrayList,以便可以轻松地将文件中的所有Client记录存储在内存中,并且每个对象都是从该文件将其放入客户端对象的array/arrayList

The requirement of the exercise is now I have to modify the program to utilise an array or arrayList of this Client object, so that all of the Client records from the file can be easily stored in memory, and each objects is created from the file place it into the array/arrayList of client objects

我刚刚开始研究array和arrayList几周,所以我不知道这样做.谁能帮我吗?

I have just begin to study array and arrayList few week, so I have no idea of doing this exercises. Can anyone help me?

推荐答案

在您的 while 循环之前,创建一个 ArrayList

Before your while loop create a ArrayList

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

在您的循环中,

Client client = new Client(clientID, firstName, surName, street, suburb, state, postcode);

// add to list
cList.add (client);
        

这篇关于使用对象的array或arrayList,以便可以将其来自文件的所有记录存储在内存中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆