gpt4 book ai didi

java - 在 java 中使用 ArrayList 的 getter 和 setter

转载 作者:太空宇宙 更新时间:2023-11-04 12:13:08 25 4
gpt4 key购买 nike

我正在尝试读取 csv 并将记录存储在 ArrayList 中。因为我知道没有。我在创建对象时指定了 csv 文件中记录的大小,即 600。我希望程序能够读取未知编号的文件。的记录。如何使其动态。

这是包含 600 条记录的文件的工作代码。

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.io.*;


public class BankRecords extends Client{
//Create objects for processing data
//private static int count;
static BankRecords[] obj=new BankRecords[600];
static List<List<String>> array = new ArrayList<List<String>>();
@Override
void readData() {
// TODO Auto-generated method stub
String line=" ";
//int i=0;

//try with resources statement
try(BufferedReader br = new BufferedReader(new FileReader("bank-Detail.csv"))){

while((line=br.readLine()) != null) //read from file
{
array.add(Arrays.asList(line.split(",")));
//check data
//count++;
//System.out.println(array.get(i++));
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

processData();
}
@Override
void processData() {
// TODO Auto-generated method stub

int idx=0;
for(List<String> bankData: array)
{

obj[idx]= new BankRecords();
obj[idx].setId(bankData.get(0));
obj[idx].setAge(Integer.parseInt(bankData.get(1)));
obj[idx].setSex(bankData.get(2));
obj[idx].setRegion(bankData.get(3));
obj[idx].setIncome(Double.parseDouble(bankData.get(4)));
obj[idx].setMarried(bankData.get(5));
obj[idx].setChild(Integer.parseInt(bankData.get(6)));
obj[idx].setCar(bankData.get(7));
obj[idx].setSact(bankData.get(8));
obj[idx].setCact(bankData.get(9));
obj[idx].setMort(bankData.get(10));
obj[idx].setPep(bankData.get(11));


idx++;

//System.out.println(obj[idx].getId());
}

printData();
}





@Override
void printData() {

//Printing First 25 ID, age, sex, region, income and mortgage
System.out.println("ID\t\tAGE\t\tSEX\t\tREGION\t\tINCOME\t\tMORTGAGE\n");
for(int i=0;i<25;i++){

String s=String.format("%s\t\t%s\t\t%s\t\t%-10s\t%8.2f\t%2s", obj[i].getId(),obj[i].getAge(),obj[i].getSex(),obj[i].getRegion(),obj[i].getIncome(),obj[i].getMort());
System.out.println(s);
}
}
public String getId() {
return id;
}




public void setId(String id) {
this.id = id;
}




public int getAge() {
return age;
}




public void setAge(int age) {
this.age = age;
}




public String getSex() {
return sex;
}




public void setSex(String sex) {
this.sex = sex;
}




public String getRegion() {
return region;
}




public void setRegion(String region) {
this.region = region;
}




public double getIncome() {
return income;
}




public void setIncome(double income) {
this.income = income;
}




public String isMarried() {
return married;
}




public void setMarried(String married) {
this.married = married;
}




public int getChild() {
return child;
}




public void setChild(int child) {
this.child = child;
}




public String getCar() {
return car;
}




public void setCar(String car) {
this.car = car;
}




public String getSact() {
return sact;
}




public void setSact(String sact) {
this.sact = sact;
}




public String getCact() {
return cact;
}




public void setCact(String cact) {
this.cact = cact;
}




public String getMort() {
return mort;
}




public void setMort(String mort) {
this.mort = mort;
}




public String getPep() {
return pep;
}




public void setPep(String pep) {
this.pep = pep;
}




public static void main(String[] args) {
// TODO Auto-generated method stub
BankRecords bnk= new BankRecords();
bnk.readData();
}

}

最佳答案

ArrayList元素可以动态变化,因此不需要提前知道大小。

但是,对于 BankRecords 数组,最初不要使用 600 对其进行初始化。相反,做这样的事情:

static BankRecords[] obj = null;
static List<List<String>> array = new ArrayList<List<String>>();
void processData() {
// TODO Auto-generated method stub
obj=new BankRecords[array.size()];
// TODO do your work here
}

关于java - 在 java 中使用 ArrayList 的 getter 和 setter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39684466/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com