由於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,...)。並使用其他類型的列表。欲瞭解更多詳情,請閱讀:
- When to use a List over an Array in Java?
- 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
使用表示兩個對象之間關聯的散列表。如果您想要的主要操作是查找任何關聯,則散列圖是首選解決方案。
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);
}
}
Java數組只使用'int's爲指標。如果你想要的話你正在尋找'Map' – Pshemo