Android Course - How to use Java Variable Array List and Map

Android Tutorial – Java Array, List and Map

Articles & Tutorials Programming

How to use Java variables Array, List, and Map?

 

1) Java Array:

Arrays are used to keep multiple values in one variable.

Using type and square brackets we declare an array variable :

Type[] arrayVariabl;

For example :

String[] friends;

Initialization:

To init a Java array, you must use the curly brace and insert values separated by a comma. For example:

int[] numbers = {1, 5, 7, 2, 0};

 

Access element of an array:

To get access to any element of an array, you must use an index between square brackets. An index represents a position of elements inside the array.

The position of the first element isn’t 1, it’s zero (0), so you want for example to get access for the third element, you need to use the position two (2).

Let’s practice this concept in a new example:

String[] friends = {Azzedine, Karim, Reda, Halim, Amine}

 

If I want to get a friend ‘Halim’, I’ll do it like this :

// Access to "Halim"

System.out.println(friends[3]);

 

Update an element of the array:

To modify a value of the element in the array, you just use the index as before, for example, I want to change the friend name “Halim” by “Said”, I’ll do it like this :

// Replace (update) "Halim" by "Said"

friends[3] = "Said";

 

Get the number of elements:

To get the number of elements in the array, you need to call the method length like this :

System.out.println("The number of elements inside friends Array is : "+ friends.length);

 

2) Java List:

 

The List is an array but it can be resizable, there is some difference between List and an Array, the size of the list isn’t fixed and the number of elements in a list can change

To use List, you need to import a package java.util.*

Here is an example of how to declare à List :

// Import the List interface
import java.util.List;
import java.util.ArrayList;

List<String> friends = new ArrayList<String>(); // Use an ArrayList for creating an object

 

Add and remove element:

As I’ve said before, you can modify the size of your list, I mean, you can add new elements or remove them.

To add a new element, you’ll use the method add(Object) with an Object or primitive type in the parameter.  Here is an example :

// Add friend Hussein to the list 'friends'

friends.add("Hussein");

To remove an element of the list, you’ll use the method remove with an index like this: list.remove(index)

For example, if I want to remove the second element of my list friends, I’ll do it like this :

friends.remove(1);

 

Access and update element:

To access to the element of the list, you need to use an index (as for Array) with the method get like this: list.get(index)

For updating an element of the list, use the method set with two parameters index and new value: list.set(index, newValue)

Example :

// get the friend "Amine", i suppose that "Amine" is the fourth element in the list

friends.get(3);

// Update the second element

friends.set(1, "Jamel");

 

The number of elements:

Just use the method size() like this: list.size();

 

3) Java Map:

In the previous section, you learned that List stored items such as a collection and you can access these items with an index (number). However, The Map (HashMap) store items with “key, value” pairs and you can access it with index not necessary a number (for example String).

To use Map (HashMap), you need to import a package java.util.*

Here is an example of how to declare a Map :

// Import the Map interface
import java.util.*;

Map<String, String> friends = new HashMap<String, String>(); // Use an HashMap for creating an object

 

Add and remove an element from the Java Map:

The Map has a similarity with List, the size isn’t fixed, so you can add and remove an element when you want.

To add a new element, you’ll use the method put(key, value) with the key that must be unique, we can’t have a duplicate key.  Here is an example of how to add an element to your Map :

// Add elements to my Map friends, Key : firstName, Value: Country

friends.put("Azzedine", "Algeria");
friends.put("Karim", "Algeria");
friends.put("Hussein", "Tunisia");
friends.put("Amine", "Morocco");
friends.put("Said", "Morocco");
friends.put("Slimane", "France");
friends.put("Karim", "Algeria"); // Not allowed, the key "Karim" exist in the Map "friends", if you keep this instruction you'll get an exception

To remove an element from the Map, you’ll use the method remove with a key like this: map.remove(key)

For example, if I want to remove the key “Karim” of my map friends, I’ll do it like this :

friends.remove("Karim");

 

Update and access element:

To access the element of the map, you need to use a key with the method get like this: map.get(key)

For updating an element of the map, use the method replace with two parameters key and new value: map.replace(key, newValue)

Example :

// get the country of the friend "Karim"

String country = friends.get("Karim");

// Update the country of "Slimane"

friends.replace("Slimane", "Algeria");

 

The number of elements:

Just use the same method size() as the List like this: map.size();

 

There a lot of methods that I can’t put them here about Array, List, and Map, I recommend you visit the Java Doc for more details.

 

Post related :

Android Tutorial – Java Variables

Android Tutorial – Your First Java Program

 

Leave a Reply

Your email address will not be published. Required fields are marked *