简体   繁体   English

从ArrayList访问索引值并将其存储在单独的新ArrayList中

[英]Accessing index values from an ArrayList and storing them in a seperate new ArrayList

图以帮助解释问题

I currently have an ArrayList called months. 我目前有一个名为months的ArrayList

ArrayList<MonthData> months;

MonthData is a class that is basically a model for data. MonthData是一个类,基本上是数据模型。

public class MonthData {

  int y;
  int m;
  float h;
  ...


  public MonthData(String data) throws Exception {
    ...
    this.parseData(data);
  }


  void parseData(String csvData) {
    String[] parseResult = csvData.trim().split("\\s+");

    this.setYear(parseResult[0]);
    this.setMonth(parseResult[1]);
    ...


  public String toString() {
    return "y =" + year + ", m =" + month + ",...

  }


  public int getY() {
    return y;
  }

  // followed by lots of getters for: m, h, c, f, r, s, ... 

Now for the second public class... 现在是第二次公开课...

public class Totals {
  private ArrayList<MonthData> months;


  public static void main(String args[]) throws IOException, Exception {
    Totals t = new Totals("..blah/../..blah/../Numbers.data");

  }

  public void readDataFile(String filename) throws IOException, Exception {
    FileReader file = new FileReader(filename);
    BufferedReader buffer = new BufferedReader(file);
    String line;

    buffer.readLine(); //skipping headers
    ...

    while (!(line = buffer.readLine()).isEmpty()) {
      this.months.add(new MonthData(line.trim()));
    }

    buffer.close();
    System.out.println(this.months);
  }

This class reads a file containing lots of data, here is a snippet of the data: 此类读取包含大量数据的文件,以下是数据片段:

     y    m      h       c       f      r       s //here for your reference

   1930   1    8.1     2.4       6   120.5    54.2
   1930   2    4.4     0.6      12    22.2    29.1
   1930   3    8.1     2.1       9    76.2    88.2
    ...

When I System.out.println(this.months); 当我System.out.println(this.months);

I get this: 我得到这个:

y=1930, m=1, h=8.1, c=2.4, f=6, r=120.5, s=54.2, y =1930, m=2, h=4.4, c=0.6, f=12, r=22.2, s=29.1, ... etc. y=1930, m=1, h=8.1, c=2.4, f=6, r=120.5, s=54.2, y =1930, m=2, h=4.4, c=0.6, f=12, r=22.2, s=29.1, ...等等。

As you can see it corresponds with the data file so I know the data is being read to the ArrayList months properly. 如您所见,它与数据文件相对应,因此我知道数据已正确地读入ArrayList months

******** QUESTION *********** Now what I want to do is look at this ArrayList and get every r value and store them in a different ArrayList, lets say ArrayList rValues (so that i have an ArrayList full of r values only). ********问题***********现在,我要做的就是看一下ArrayList并获取每个r值并将它们存储在不同的ArrayList中,比如说ArrayList rValues (因此我有一个只有r值的ArrayList)。

I know I need to iterate over this ArrayList somehow to the r value indexes, get the values and then store them in another ArrayList just dont know how!! 我知道我需要以某种方式遍历此ArrayList到r值索引,获取值,然后将它们存储在另一个ArrayList中,只是不知道如何! :( :(

Any help here at all will be greatly appreciated. 在这里的任何帮助将不胜感激。 Happy to answer any questions, although i have possibly explained whats going on as best as i can. 很高兴回答任何问题,尽管我可能已尽我所能解释了一切。 Thanks in advance guys :) 在此先感谢大家:)

Why don't you iterate over list like this: 您为什么不遍历这样的列表:

for (int i = 0; i<months.size(); i++)

and then you can get your MonthData object with this command 然后您可以使用此命令获取MonthData对象

months.get(i)

and if you want just ar value then create getter for r ( getR() ) and call it and save in new array list: 如果只需要ar值,则为r( getR() )创建getter并调用它并保存在新数组列表中:

Something like this: 像这样:

ArrayList<Float> rValue = new ArrayList<>();
for (int i = 0; i<months.size(); i++)
{
    rValue.add(months.get(i).getR());
}

(thanks to @Mick Mnemonic) You can also use foreach loop (感谢@Mick助记符)您也可以使用foreach循环

ArrayList<Float> rValue = new ArrayList<>();
for (MonthData m: months) 
{ 
    rValue.add(m.getR()); 
}

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

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