繁体   English   中英

从java中的列表中获取列表?

[英]Get list from lists in java?

首先,我需要说我尝试在互联网上找到解释,但对我来说都不清楚。

我在 java FX 中编写了一个程序。 其中一个类具有生成两个列表的方法:

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]

我需要返回该列表,以便将其连接为一个:

ArrayList<List<Integer>> result = new ArrayList<List<Integer>>();
        result.add(listA);
        result.add(listB);
        return(result);

该方法的负责人看起来:

static public List man() { #could be usefull to explain me how to fix my code.

所以现在在另一个类中我调用该方法:

List newList= classTest.man();

并打印它:

System.out.println(newList);

所以我得到了:

[[0,2,3,4,1,2,5,12,3,2,1,3][2,3,4,1,2,1,2,3,1,2,3,4]]

没关系,我只想写一个循环来为我的图表填充数据:

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

但首先:

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'"

请问有人能帮我解决我的问题吗? 我写了几乎整个程序,并卡在这里。

长度为红色:错误 =“无法解析符号‘长度’”

.length用于获取数组的长度。 我假设您正在使用java.util.List 您需要使用方法size ,例如newList.get(0).size()

在 newList.get(0).get(i) 中,第二个 .get(i) 是红色的。 错误 =“无法解析‘对象’中的‘get’方法”

static public List man()我不确定这个方法是做什么的,但你使用的是原始类型。 这意味着您返回的List基本上等同于List<Object>

因此,您正在尝试调用Object#get(i) ,并且get不是对象上的有效方法。 您需要指定List man()将返回的类型。 例如, List<Integer> man()或它需要的任何类型。 我无法从您的代码中看出您实际在做什么。

  1. “.length”用于确定数组的长度。 对于列表,您使用“.size()”

  2. 尝试将其替换为((List<Integer>) newList.get(0)).get(i);

暂无
暂无

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

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