问题描述
我有一个 Line
类,它包含一个内部 ArrayList
.在我的程序中,我有一个 ArrayList
包含许多 Line
对象.
I have a class Line
that contains an internal ArrayList<Double>
. In my program, I have an ArrayList<Line>
that contains many Line
objects.
在我的程序中,我希望能够创建一个新的 ArrayList
,其中包含来自所有 的
在我的 ArrayList
的内容LineArrayList
中.有没有简单的方法可以做到这一点?
In my program, I want to be able to create a new ArrayList<Double>
that contains the content of the ArrayList<Double>
from all the Line
s in my ArrayList<Line>
. Is there an easy way to do this?
这是我的代码...
class Line {
ArrayList<Double> values;
line() {
values = new ArrayList<Double>();
}
public class Calucating{
ArrayList<Line> lineY = new ArrayList<Line>();
// Adding lines in here...
// Now I want to add the contents of each Line into a single ArrayList<Double>
ArrayList<Double> lineNewY=new ArrayList<Double>;
}
如何轻松地将所有 Line
的内容合并为一个 ArrayList
?
How can I easily join the content of all my Line
s into a single ArrayList<Double>
?
推荐答案
所以你有一个行数组,每行包含一个双精度数组,而你想要得到一个包含所有双精度数组的大型双精度数组行中的数组.
So you have an array of lines, each line containing an array of doubles, and you want to get one large array of doubles that contains all the doubles in all the arrays in the lines.
class Line {
ArrayList<Double> values = new ArrayList<Double>();
}
public class Calculating {
ArrayList<Line> lineY = new ArrayList<Line>();
void someMethod() {
ArrayList<Double> lineNewY = new ArrayList<Double>;
for( Line line : lineY )
lineNewY.addAll( line.values );
// lineNewY now has all the doubles
}
}
这篇关于转换数组列表类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!