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

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

问题描述

我是 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

这是我的客户端类(有构造函数):

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 对象的一个​​数组或 arrayList,以便文件中的所有 Client 记录可以很容易地存储在内存中,并且每个对象都是从该文件将其放入客户端对象的数组/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

我几周才开始学习数组和数组列表,所以我不知道做这个练习.有人可以帮我吗?

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

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

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