简体   繁体   English

如何将ArrayList中索引中的数据存储到Java中的数组中

[英]How to store data in an index in an ArrayList into Arrays in Java

I am new to Java and I am trying to do a calculation using values within a txt file.我是 Java 新手,我正在尝试使用 txt 文件中的值进行计算。 I have read the txt file, which has 3 tabs, each with three values我已经阅读了 txt 文件,它有 3 个选项卡,每个选项卡都有三个值

I have been able to read the file and get the columns as indices but cannot add the separate values into array.我已经能够读取文件并将列作为索引,但无法将单独的值添加到数组中。 So I want three separate arrays所以我想要三个独立的数组

Read file method读取文件方法

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) {}
}

Processing method加工方法

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

    }
}

I dont think you are thinking about your data structures correctly.我认为您没有正确考虑您的数据结构。 If I were you I would think about this a little differently.如果我是你,我会以不同的方式思考这个问题。 To me it makes the most sense just to use a simple array.对我来说,使用一个简单的数组是最有意义的。 To handle the complexity of the three columns, I would create a new class called CustomerRate or something to that effect.为了处理三列的复杂性,我将创建一个名为 CustomerRate 的新类或类似的东西。 I would then make the data into instance variables belonging to instances of that class.然后,我会将数据转换为属于该类实例的实例变量。 That way you could just have a simple array of CustomerRate objects and then access the data stored by each of those objects.这样您就可以拥有一个简单的 CustomerRate 对象数组,然后访问每个对象存储的数据。 This will probably be a lot simpler to deal with overall too.这在整体上处理起来也可能简单得多。

I am not sure exactly what you are trying to accomplish but I'll do my best to help You would create your new class to be something like this: your new 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;
        }
    }

Use the data your parse from your file to create new CustomerRate Objects.使用您从文件中解析的数据来创建新的 CustomerRate 对象。 Create an array of your objects.创建一个对象数组。 Note that this is just an example with one entry with random numbers I'm going to use so you will have to get the loop and parse working:请注意,这只是一个示例,其中包含一个我将要使用的随机数条目,因此您必须使循环和解析工作:

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

I coded this quickly so I may have made some mistake but I hope that gives you the general idea of what you should do.我很快就编写了代码,所以我可能犯了一些错误,但我希望这能让您大致了解应该做什么。

You just need another ArrayList to store your rateOfBusiness .您只需要另一个ArrayList来存储您的rateOfBusiness Something like this:像这样的东西:

String file = "test.txt";
ArrayList<String> a = new ArrayList<String>();
ArrayList<Double> rateOfBusiness = new ArrayList<>();  //Define with your other fields

Then loop through your data and do the math while adding to the array然后遍历您的数据并在添加到数组的同时进行数学运算

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
    }
}

Edit: Even though this solves your problem, I would look into learning some Object Oriented principles.编辑:即使这解决了您的问题,我还是会考虑学习一些面向对象的原则。 IsaacShiffman (or Lvl 9 Oddish, or whatever his name is) has a start on how you would solve this going that direction. IsaacShiffman(或 Lvl 9 Oddish,或不管他的名字是什么)有一个关于如何解决这个方向的开始。 Makes your code a lot easier to follow and debug.使您的代码更易于跟踪和调试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM