2015-05-18 110 views
-3

我需要的東西,在PHP會看(二維數組):如何在Java中創建數組?

Array 
(
[0] => Array 
    (
     [name] => foo 
     [desc] => some foo 
    ) 

[1] => Array 
    (
     [name] => bar 
     [desc] => some bar 
    ) 

) 

所有的Java例子說,我應該創建「整數[] []數據;」或「String [] [] data;」但在這裏我有Int(索引)和String ...如何混合它? 如何填充它... datas [i] [「name」] =「aa」? 如何喜歡讀它在另一個循環:

for(int i=start;i<start;i++) 
{ 
    value = data[i]["name"]...??? 
} 
+4

Java數組只使用'int's爲指標。如果你想要的話你正在尋找'Map' – Pshemo

回答

0

由於Pshemo說,Java的使用僅INT作爲索引數組。

你有很多解決方案來實現你的目標。我將只展示它們的樹,都使用一個對象容器。 Java是對象編程,使用它們。

// Class declaration 
public class SimpleContainer { 

    private final String name; 
    private final String description; 

    public SimpleContainer(final String name, final String description) { 
     this.name = name; 
     this.description = description; 
    } 

    public String getName() { 
     return name; 
    } 

    public String getDescription() { 
     return description; 
    }} 

1,使用對象的陣列(不推薦):

SimpleContainer[] simpleContainers = new SimpleContainer[3]; 
simpleContainers[0] = new SimpleContainer("name 1", "description 1"); 
simpleContainers[1] = new SimpleContainer("name 2", "description 2"); 
simpleContainers[2] = new SimpleContainer("name 3", "description 3"); 

for (int i = 0; i < simpleContainers.length; i++) { 
     System.out.println(String.format("I am [%s] and my description is [%s]", 
     simpleContainers[i].getName(), 
     simpleContainers[i].getDescription())); 
} 

  • 使用一個列表,而不是您的陣列的
  • 在Java中,經驗法則是僅對原始類型使用數組(如int,byte s,...)。並使用其他類型的列表。欲瞭解更多詳情,請閱讀:

    1. When to use a List over an Array in Java?
    2. more

    列表的使用(同一個對象)的一些簡單的例子:

    List<SimpleContainer> myList = new ArrayList<SimpleContainer>(); 
    myList.add(new SimpleContainer("name 1", "description 1")); 
    myList.add(new SimpleContainer("name 2", "description 2")); 
    myList.add(new SimpleContainer("name 3", "description 3")); 
    
    // Use iterator (support remove object for most list implementations). 
         Iterator<SimpleContainer> it = myList.iterator(); 
    
    while(it.hasNext()) { 
        SimpleContainer simpleContainer = it.next(); 
        System.out.println(String.format("I am [%s] and my description is [%s]", 
        simpleContainer.getName(), 
        simpleContainer.getDescription())); 
    } 
    
    // Or simple loop. 
    for (SimpleContainer simpleContainer : myList) { 
        System.out.println(String.format("I am [%s] and my description is [%s]", 
        simpleContainer.getName(), 
        simpleContainer.getDescription())); 
    } 
    

    瞭解藏品在Java中是非常重要的,只需檢查如下教程: Simple tutorial about collections


    1. 使用表示兩個對象之間關聯的散列表。如果您想要的主要操作是查找任何關聯,則散列圖是首選解決方案。

      HashMap<String, String> myMap = new HashMap<>(); 
      myMap.put("name 1", "description 1"); 
      myMap.put("name 2", "description 2"); 
      myMap.put("name 3", "description 3"); 
      
      String description = myMap.get("name 1"); 
      
      Iterator<Map.Entry<String, String>> it = myMap.entrySet().iterator(); 
      
          while (it.hasNext()) { 
      
          Map.Entry pair = (Map.Entry)it.next(); 
          System.out.println(pair.getKey() + " = " + pair.getValue()); 
      } 
      

  • 也許要使用與int和string雙指數,在這種情況下,可以使用該頁面的一個示例: Double key indexing

  • 最後可以使用謂詞(java的8或瓜VA圖書館)。有一個使用番石榴謂詞的例子。

    public void ExamplePredicate() { 
    List<SimpleContainer> myList = new ArrayList<SimpleContainer>(); 
    myList.add(new SimpleContainer("name 1", "description 1")); 
    myList.add(new SimpleContainer("name 2", "description 2")); 
    myList.add(new SimpleContainer("name 3", "description 3")); 
    
    // Get one index : 
    System.out.println(myList.get(1).getDescription()); 
    
    // Try to find an entry with name. 
    Optional<SimpleContainer> optContainer = Iterables.tryFind(myList, new NameContainerPredicate("name 1")); 
    if (optContainer.isPresent()) { 
        System.out.println(optContainer.get().getDescription()); 
    } 
    
    // Try to find an entry with name between two index. 
    Optional<SimpleContainer> optContainer2 = Iterables.tryFind(myList.subList(1, 2), new NameContainerPredicate("name 1")); 
    if (optContainer2.isPresent()) { 
        System.out.println(optContainer2.get().getDescription()); 
    } else { 
        System.out.println("No entry found"); 
    } 
    
    // Get all entry with given name. 
    List<SimpleContainer> containersList = Lists.newArrayList(Iterables.filter(myList.subList(1, 2), new NameContainerPredicate("name 1"))); 
    for (SimpleContainer simpleContainer : containersList) { 
        System.out.println(String.format("Name [%s] description [%s]", simpleContainer.getName(), simpleContainer.getDescription())); 
    } 
    } 
    
    private static class NameContainerPredicate implements Predicate<SimpleContainer> { 
    
    private final String name; 
    
    public NameContainerPredicate(final String name) { 
        this.name = name; 
    } 
    
    @Override 
    public boolean apply(@Nullable final SimpleContainer input) { 
        return input != null && equal(input.getName(), name); 
    } 
    } 
    
  • +0

    或者只是使用一個hashmap,因爲它代表了一個「關聯數組」,這是PHP使用的。 – Bauss

    +0

    謝謝你們。如果我讀得好你的1-2解決方案Manticore提供簡單的列表沒有更多。我需要二維數組。 HashMap很有趣,但我找不到如何從中獲取具體索引的示例;我在PHP中用array [index] [key]做什麼。例如,我想要從Hashmap的第三個索引.... –

    +0

    你有一個使用的例子,因爲我認爲你想要一個數組,但你不需要一個。 – Manticore