[Java] Array, ArrayList, and HashMap

Array

1
2
3
4
5
6
7
int[] myArray = {1, 2, 3, 4, 5};
String[] myStringArray = {"Hello", "Hi", "What"};

System.out.println(myStringArray.length);

int[] newArray = new int[5];
String[] newStringArray = new String[2];

ArrayList

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ArrayList<String> animals = new ArrayList();
animals.add("Pig");
animals.add("Cow");
animals.add("Parrot");

animals.get(0);
animals.remove(0);
animals.add(0, "Dog");

animals.remove("Cow");

animals.contains("Chicken");

for (String animal: animals) {
System.out.println(animal)
}

HashMap

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
HashMap users = new HashMap();
users.put("Alex", 23);
users.put("Paul", 18);

System.out.println(users.entrySet());
System.out.println(users.get("Alex"));
users.remove("Paul");

Iterator iterator = users.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry pair = (Map.Entry) iterator.next();
System.out.println("Key: " + pair.getKey() + "Value: " + pair.getValue());
}

HashMap<Integer, String> newUsers = new HashMap<>();
newUsers.put(10, "Alex");