如何从嵌套的ArrayList中访问特定元素

huangapple go评论68阅读模式
英文:

How do I access a specific element from a nested ArrayList

问题

我有两个ArrayLists

private ArrayList<ArrayList<String>> dataList = new ArrayList<>();
//这是一个类变量

ArrayList<String> dataRow = new ArrayList<>();
//这是一个方法变量

我向dataRow添加项目

dataRow.add("number");
dataRow.add("firstName");
dataRow.add("surname");
dataRow.add("dateStart");
dataRow.add("dateEnd");

然后我将每个dataRow添加到dataList中,从而得到一个ArrayList的ArrayLists

dataList.add(dataRow);

我的问题是:

我需要从每个dataRow中选择第3和第4个元素,我找不到任何可行的代码。

我尝试过

for (ArrayList<String> eachRow : dataList)
{
    for (String eachElement : eachRow)
    {
        System.out.println(eachElement);
    }
}

这只会打印出所有的元素。

我还尝试过其他代码,比如

dataList.get(eachElement)

这在NetBeans中会引发一个没有合适的方法错误。

英文:

I have two ArrayLists

private ArrayList&lt;ArrayList&lt;String&gt; dataList = new ArrayList&lt;&gt;();
//This is a class variable

ArrayList&lt;String&gt; dataRow = new ArrayList&lt;&gt;();
//This is a method variable

I add items to dataRow

dataRow.add(&quot;number&quot;);
dataRow.add(&quot;firstName&quot;);
dataRow.add(&quot;surname&quot;);
dataRow.add(&quot;dateStart&quot;);
dataRow.add(&quot;dateEnd&quot;);

and then I add each dataRow to dataList resulting in an ArrayList of ArrayLists

dataList.add(dataRow);

My Question is:

I need to select just elements 3 and 4 from each dataRow and I can't find any code that works.

I have tried

for (ArrayList&lt;String&gt; eachRow : dataList)
{
    For (String eachElement : eachRow)
    (
        System.out.println(eachElement)
    }
}

All this does is print out all the elements

I have also tried other code such as

dataList.get(eachElement)

this throws a no suitable method error in netbeans

答案1

得分: 0

我一发布这个帖子就解决了。

内部循环中的代码应为:

System.out.println(eachRow.get(4));
英文:

I worked it out as soon as I had posted this.

The code in the inner for loop should be:

System.out.println(eachRow.get(4));

</details>



# 答案2
**得分**: 0

假设您只想提取两个数组元素,您可以使用一个简单的POJO来实现:

```java
class YourPojo { // 将其重命名为自解释的名称
  private String field3;
  private String field4;   
  // getters &amp; setters
}

然后定义一个实用方法,从原始数组中提取数据到您的POJO中:

static YourPojo extractYourPojo(List<String> rawData){
    YourPojo pojo = new YourPojo();
    pojo.setField3(rawData.get(3));
    pojo.setField4(rawData.get(4));
    return pojo;
} 

然后您可以按以下方式使用该方法:

List<YourPojo> extracted = 
    dataList.stream()
            .map(ClassWithUtilityMethod::extractYourPojo)
            .collect(toList());
英文:

Assuming you want to extract only two array elements, you could use a simple POJO for that:

class YourPojo { // rename it to something self-explanatory
  private String field3;
  private String field4;   
  // getters &amp; setters
}

Then define an utility method extracting the data into your POJO from the raw array:

static YourPojo extractYourPojo(List&lt;String&gt; rawData){
    YourPojo pojo = new YourPojo();
    pojo.setField3(rawData.get(3));
    pojo.setField4(rawData.get(4));
    return pojo;
} 

Then you can use the method as follows:

List&lt;YourPojo&gt; extracted = 
    dataList.stream()
            .map(ClassWithUtilityMethod::extractYourPojo)
            .collect(toList());

答案3

得分: 0

你可以创建一个类来存储像下面这样的值,

class ExtractedValue {

    private String value3;
    private String value4;

    public ExtractedValue(String value3, String value4) {
        this.value3 = value3;
        this.value4 = value4;
    }

    public String getValue3() {
        return value3;
    }

    public String getValue4() {
        return value4;
    }
}

并且可以使用以下代码来提取第三和第四个值,

List<ExtractedValue> extracted = dataList.stream().map(l -> new ExtractedValue(l.get(3), l.get(4)))
        .collect(Collectors.toList());

要打印,你可以使用以下代码,

extracted.stream().forEach(e -> {
    System.out.println(e.getValue3());
    System.out.println(e.getValue4());
});
英文:

You can have a class to store values like below,

class ExtractedValue {

  private String value3;
  private String value4;

  public ExtractedValue(String value3, String value4) {
	this.value3 = value3;
	this.value4 = value4;
  }

  public String getValue3() {
	return value3;
  }

  public String getValue4() {
	return value4;
  }
}

And use the below to extract the 3rd and 4th values,

List&lt;ExtractedValue&gt; extracted= dataList.stream().map(l -&gt; new ExtractedValue(l.get(3), l.get(4)))
			.collect(Collectors.toList());

To print you can use the below,

extracted.stream().forEach(e -&gt; {
		System.out.println(e.getValue3());
		System.out.println(e.getValue4());
	});

huangapple
  • 本文由 发表于 2020年4月3日 20:55:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/61012399.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定