簡體   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