如何将 ArrayList 中索引中的数据存储到 Java 中的数组中
How to store data in an index in an ArrayList into Arrays in Java
我是 Java 的新手,我正在尝试使用 txt 文件中的值进行计算。我已经阅读了 txt 文件,它有 3 个选项卡,每个选项卡有三个值
我已经能够读取文件并将列作为索引,但无法将单独的值添加到数组中。所以我想要三个独立的数组
读取文件方法
public void read()
{
try
{
FileReader read = new FileReader(file);
String line = null;
while((line = FileReader.readLine()) != null)
{
a.add(line);
}
}
catch (FileNotFoundException e) {}
catch(IOException e) {}
}
加工方式
private void processor () {
for (String li : a)
{
String[] data = li.split("\s");
Index = Double.parseDouble(data[0]);
Customers = Double.parseDouble(data[1]);
rateOfBuy = Double.parseDouble(data[2]);
}
}
我认为您没有正确考虑数据结构。如果我是你,我会以不同的方式思考这个问题。对我来说,使用一个简单的数组是最有意义的。为了处理这三列的复杂性,我将创建一个名为 CustomerRate 或具有类似效果的新 class。然后我会将数据变成属于 class 实例的实例变量。这样您就可以拥有一个简单的 CustomerRate 对象数组,然后访问每个对象存储的数据。这也可能更容易处理。
我不确定你到底想完成什么,但我会尽力帮助
你会创建你的新 class 是这样的:
你的新 class:
//This is your new class
public class CustomerRate {
private int person;
private double rate;
//constructor
public CustomerRate(int person, double rate) {
this.person = person;
this.rate = rate;
}
//add appropriate getters and setters and whatever else you need
public double getRate() {
return rate;
}
}
使用您从文件中解析的数据创建新的 CustomerRate 对象。创建一个对象数组。请注意,这只是我将要使用的一个带有随机数的条目的示例,因此您必须让循环和解析正常工作:
//creating an example customer
CustomerRate customer1 = new CustomerRate(100, 0.5);
//create your collection to store your customer data that you will add/parse
List<CustomerRate> myList = new ArrayList<CustomerRate>();
//adds to list
myList.add(customer1);
//gets element at index and then grabs the rate
double exampleCustomerRate;
exampleCustomerRate = myList.get(0).getRate();
我编写代码的速度很快,所以我可能犯了一些错误,但我希望这能让您大致了解应该做什么。
您只需要另一个 ArrayList
来存储您的 rateOfBusiness
。像这样:
String file = "test.txt";
ArrayList<String> a = new ArrayList<String>();
ArrayList<Double> rateOfBusiness = new ArrayList<>(); //Define with your other fields
然后循环遍历您的数据并在添加到数组的同时进行数学运算
private void process () {
for (String li : a)
{
String[] data = li.split("\t");
Index = Double.parseDouble(data[0]);
Customers = Double.parseDouble(data[1]);; //per year
rateOfBuy = Double.parseDouble(data[2]); //per year
rateOfBusiness.add(Customers*rateOfBuy); //Do math and store for that customer
}
}
编辑: 尽管这可以解决您的问题,但我还是会考虑学习一些面向对象的原则。 IsaacShiffman(或 Lvl 9 Oddish,或无论他的名字是什么)已经开始研究如何解决这个问题。使您的代码更易于跟踪和调试。
我是 Java 的新手,我正在尝试使用 txt 文件中的值进行计算。我已经阅读了 txt 文件,它有 3 个选项卡,每个选项卡有三个值
我已经能够读取文件并将列作为索引,但无法将单独的值添加到数组中。所以我想要三个独立的数组
读取文件方法
public void read()
{
try
{
FileReader read = new FileReader(file);
String line = null;
while((line = FileReader.readLine()) != null)
{
a.add(line);
}
}
catch (FileNotFoundException e) {}
catch(IOException e) {}
}
加工方式
private void processor () {
for (String li : a)
{
String[] data = li.split("\s");
Index = Double.parseDouble(data[0]);
Customers = Double.parseDouble(data[1]);
rateOfBuy = Double.parseDouble(data[2]);
}
}
我认为您没有正确考虑数据结构。如果我是你,我会以不同的方式思考这个问题。对我来说,使用一个简单的数组是最有意义的。为了处理这三列的复杂性,我将创建一个名为 CustomerRate 或具有类似效果的新 class。然后我会将数据变成属于 class 实例的实例变量。这样您就可以拥有一个简单的 CustomerRate 对象数组,然后访问每个对象存储的数据。这也可能更容易处理。
我不确定你到底想完成什么,但我会尽力帮助 你会创建你的新 class 是这样的: 你的新 class:
//This is your new class
public class CustomerRate {
private int person;
private double rate;
//constructor
public CustomerRate(int person, double rate) {
this.person = person;
this.rate = rate;
}
//add appropriate getters and setters and whatever else you need
public double getRate() {
return rate;
}
}
使用您从文件中解析的数据创建新的 CustomerRate 对象。创建一个对象数组。请注意,这只是我将要使用的一个带有随机数的条目的示例,因此您必须让循环和解析正常工作:
//creating an example customer
CustomerRate customer1 = new CustomerRate(100, 0.5);
//create your collection to store your customer data that you will add/parse
List<CustomerRate> myList = new ArrayList<CustomerRate>();
//adds to list
myList.add(customer1);
//gets element at index and then grabs the rate
double exampleCustomerRate;
exampleCustomerRate = myList.get(0).getRate();
我编写代码的速度很快,所以我可能犯了一些错误,但我希望这能让您大致了解应该做什么。
您只需要另一个 ArrayList
来存储您的 rateOfBusiness
。像这样:
String file = "test.txt";
ArrayList<String> a = new ArrayList<String>();
ArrayList<Double> rateOfBusiness = new ArrayList<>(); //Define with your other fields
然后循环遍历您的数据并在添加到数组的同时进行数学运算
private void process () {
for (String li : a)
{
String[] data = li.split("\t");
Index = Double.parseDouble(data[0]);
Customers = Double.parseDouble(data[1]);; //per year
rateOfBuy = Double.parseDouble(data[2]); //per year
rateOfBusiness.add(Customers*rateOfBuy); //Do math and store for that customer
}
}
编辑: 尽管这可以解决您的问题,但我还是会考虑学习一些面向对象的原则。 IsaacShiffman(或 Lvl 9 Oddish,或无论他的名字是什么)已经开始研究如何解决这个问题。使您的代码更易于跟踪和调试。