[英]Get list from lists in java?
First, I need to say that I tried found explanation in internet, but all are unclear for me.首先,我需要说我尝试在互联网上找到解释,但对我来说都不清楚。
I worte a program in java FX.我在 java FX 中编写了一个程序。 One of classes has method generating two list:
其中一个类具有生成两个列表的方法:
listA = [0,2,3,4,1,2,5,12,3,2,1,3]
listB = [2,3,4,1,2,1,2,3,1,2,3,4]
I need to return that lists so I connect it in one:我需要返回该列表,以便将其连接为一个:
ArrayList<List<Integer>> result = new ArrayList<List<Integer>>();
result.add(listA);
result.add(listB);
return(result);
head of that method looks that:该方法的负责人看起来:
static public List man() { #could be usefull to explain me how to fix my code.
So now in another class i call the method:所以现在在另一个类中我调用该方法:
List newList= classTest.man();
And print it:并打印它:
System.out.println(newList);
So I got:所以我得到了:
[[0,2,3,4,1,2,5,12,3,2,1,3][2,3,4,1,2,1,2,3,1,2,3,4]]
And it's fine, I just want a write a loop to fill data for my chart:没关系,我只想写一个循环来为我的图表填充数据:
for(int i; i < newList.get(0).length(); i++){
String iterationNum= Integer.toString(newList.get(0).get(i);
series.getData().add(new XYChart.Data<String, Number>(iterationNum; newList.get(0).get(i)));
But first:但首先:
1. .length() is red : error = "Cannot resolve symbol 'length'"
2. in newList.get(0).get(i) the second .get(i) is red. error = "Cannot resolve method 'get' in 'Object'"
Please, can someone help me solve my problem?请问有人能帮我解决我的问题吗? I wrote almost whole of program, and stuck here.
我写了几乎整个程序,并卡在这里。
length is red : error = "Cannot resolve symbol 'length'"
长度为红色:错误 =“无法解析符号‘长度’”
.length
is used to get the length of an array. .length
用于获取数组的长度。 You are using, I assume, java.util.List
.我假设您正在使用
java.util.List
。 You need to use the method size
, eg newList.get(0).size()
您需要使用方法
size
,例如newList.get(0).size()
in newList.get(0).get(i) the second .get(i) is red.
在 newList.get(0).get(i) 中,第二个 .get(i) 是红色的。 error = "Cannot resolve method 'get' in 'Object'"
错误 =“无法解析‘对象’中的‘get’方法”
static public List man()
I'm not sure what this method does, but you are using a raw type. static public List man()
我不确定这个方法是做什么的,但你使用的是原始类型。 This means that the List
you are getting back is basically equivalent to List<Object>
.这意味着您返回的
List
基本上等同于List<Object>
。
So you are trying to call Object#get(i)
, and get
is not a valid method on an Object.因此,您正在尝试调用
Object#get(i)
,并且get
不是对象上的有效方法。 You need to specify what type List man()
will return.您需要指定
List man()
将返回的类型。 For instance, List<Integer> man()
, or whatever type it needs to be.例如,
List<Integer> man()
或它需要的任何类型。 I can't tell from your code what you're actually doing.我无法从您的代码中看出您实际在做什么。
".length" is used to determine the Length of an array. “.length”用于确定数组的长度。 For lists you use ".size()"
对于列表,您使用“.size()”
Try replacing it with ((List<Integer>) newList.get(0)).get(i);
尝试将其替换为
((List<Integer>) newList.get(0)).get(i);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.