2017-10-05 40 views
0

我有這個在Javascript:翻譯的JavaScript數組轉換成陣列插入到Java

var population = []; 
for(i=0; i < size; i++){ 
    population[i] = functionWhichReturnsArrays(); 
} 

包含不同指數的多個陣列的一維陣列 - 這就是我要的,而不是一個二維數組。

如何將其轉換爲Java?

+1

你不(用數組)。 Javascript有動態數組。 Java沒有。 –

+0

在Java中執行此操作的另一種方法是什麼? –

+1

'Object [] population = new Object [size];'創建數組,然後'for'循環類似於JS的來填充它。 –

回答

1

這讓您對如何處理它的Java(代碼就可以寫了很多票友,但是這很容易遵循)一個想法:

public class Pop { 

public static void main(String[] args) { 
    new Pop().build(10); 
} 

private void build(int size) { 
    String[][] population = new String[size][]; 
    for (int i = 0; i < size; i++) { 
     population[i] = functionWhichReturnsArrays(); 
    } 

    for (int a = 0; a < population.length; a++) { 
     for (int b = 0; b < population[a].length; b++) { 
      System.out.print(population[a][b] + "/"); 
     } 
     System.out.println(); 
    } 
} 

private String[] functionWhichReturnsArrays() { 
    String[] strs = new String[2]; 
    strs[0] = "zero"; 
    strs[1] = "one"; 
    return strs; 
} 

} 

結果將是10行:

zero/one/
zero/one/
... 
zero/one/
0

TL;博士

使用ArrayList。

回答

Java提供了大量的數據結構以供選擇,內置的Collections。 決定使用哪一個高度依賴於特定用例的性質。 通常它是靈活性和性能之間的折衷。

從稀疏描述你提供我假設你 要求的數據結構都或多或少以下

  • 不知道或關心的元素
  • 的數據結構的數保持元件
  • ,需要由索引

訪問元素的順序而後者兩者由Java一個fullfilled由於數組具有固定的大小,所以第一個不是。 除非有更具體的要求,我建議使用ArrayList

作爲一個例子,我在somethingThatReturnsAList()中創建了隨機數列表和隨機長度列表。 因爲您可以在此處使用除Numbers以外的其他值類型。 另外,應該可以爲每個列表條目使用完全不同的值類型。

您請求的列表清單最後在main方法中創建。

printThat()只是在StringBuilder的幫助下打印列表中列表 的每個索引的數字列表的內容。

import java.util.ArrayList; 
import java.util.List; 

public class ListTest { 

    public static List<Number> somethingThatReturnsAList() { 
    int   count; 
    int   value; 
    List<Number> result; 

    // Random number of elements. 
    count = (int) (Math.random() * 10); 
    result = new ArrayList<Number>(count); 

    for (int i = 0; i < count; ++i) { 
     // Random value. 
     value = (int) (Math.random() * 10); 
     result.add(value); 
    } 

    return result; 
    } 

    public static void printThat(List<List<Number>> listOfLists) { 
    StringBuilder bldr; 
    List<Number> subList; 

    bldr = new StringBuilder(); 

    bldr.append("The contents:\n"); 

    for (int i = 0; i < listOfLists.size(); ++i) { 

     // Top list index. 
     bldr.append(i).append(": "); 
     subList = listOfLists.get(i); 

     // List sub list values. 
     for (int j = 0; j < subList.size(); ++j) { 
     if (j > 0) { 
      bldr.append(", "); 
     } 
     bldr.append(subList.get(j)); 
     } 

     System.out.println(bldr.toString()); 

     bldr.setLength(0); 
    } 
    } 

    public static void main(String[] args) { 
    int     size; 
    List    subList; 
    List<List<Integer>> listOfLists; 

    size  = 5; 

    // Create that list of lists 
    listOfLists = new ArrayList<List<Integer>>(5); 
    for (int i = 0; i < size; ++i) { 
     subList = somethingThatReturnsAList(); 
     listOfLists.add(subList); 
    } 

    // Print that list of lists 
    printThat(listOfLists); 
    } 
} 

這將打印類似:

The contents: 
0: 5, 1, 2 
1: 
2: 4, 2, 9, 3, 0, 7, 6 
3: 0, 1, 5, 7, 6, 5, 1, 4, 5 
4: 7 
0
// var population = []; 
ArrayList<TYPE_RETURNED_FROM_FUNTION> population = new ArrayList<>(); 

for (int i = 0; i < size; i++) { 
    // adds at the end of the array Liste; wich is i 
    population.add(functionWhichReturnsArrays()); 
} 
+0

我相信'TYPE_RETURNED_FROM_FUNTION'必須是一個數組/列表,所以你可以在這裏更具體。 –