Arrays In Android Studio | ArrayList, HashMap And ArrayMap In Android Studio

Arrays in Android or Java. If you want to learn about array and basics of array then you are at right place. We will tell you what is array, what is use of arrays and how many types of arrays.

Arrays in Android studio

Arrays are a data structures which are used to add and get data. These are a type of collection in which you can add values or get values. You will have to use arrays in Android studio regularly because it is a function that make your work easy. Now let us understand the type of arrays you can use in Android studio.

Simple array

One of the most basic array are written as follow


String[] myStringArray;

There arrays can be float, double, ImageView, And many other things. Now lets understand how you can add and get data from these arrays.


String[] stringArray = new String
[3];

Here number 3 is indicating the number of items in array. Now lets put items in this array.


String[] stringArray = new String
[3];
stringArray[0] = "Apple";
stringArray[1] = "Ball";
stringArray[0] = "Cat";

Now your items are saved in stringArray[] and you can get them letter. But remember that as we defined a number 3 above. You can not add more than 3 items in that arrays. Now if you want to get the data, you can simply write the variable followed by index of item in square brackets. Let’s see the example.

stringArray[0] – This will return “Apple” because Apple is at zeroth index. similarly
stringArray[1] will return “Ball”

Now if you try to access 4th item in that string then it will cause error. If you want to get the total number of items in this type of arrays then you can get it by stringArray.length

ArrayList

ArrayList are also array use in Android studio. But adding and getting item is different. Also you don’t need to define the length of array at beginning. Lets see the example.


ArrayList myArray = new ArrayList();
myArray.add("Apple");
myArray.add("Ball");
myArray.add("Cat");

You can also put value at any index. for example


myArray.add(1,"Boy");

This will add a value “Boy” at first index which is second position.

Now lets try to get a value from this ArrayList.

myArray.get(0); will return the “Apple” which is at zero position of array. Same as simple arrays, do not try to acess item out of range. If you want to get the length of items in ArrayList them just type size() after variable. For example myArray.size() will give you the number of items in myArray.

Also you can define the type of ArrayList so that only that type of object will be allowed to add. Lets make a ArrayList which only allow integers.


ArrayList<Integer> intArray = new ArrayList<Integer>();

now you can add only integer values in above array. When you try to add a string then it will cause error. Similarly you can make array of ImageView, or any other things.

HashMap or ArrayMap

HashMap and ArrayMap are special arrangements that use key-value pair. It means you can save a value for a key. Let’s make things clear with example.


HashMap map = new HashMap();

map.put("First","Apple");
map.put("Second","Ball");

Here we are putting a key “First” with a value “Apple”. Now we can access the hashmap and use it later. For getting value of the key, just use get method. Lets see the example.

map.put(“First”) will return “Apple”.

Remember that you can not get values from index like ArrayList. Also you can set the type of key and value in HashMap which only allow to add special type of objects, for example.


HashMap<String,Integer> map = new HashMap<String,Integer>();
map.put("First",100);
map.put("Second",200);

In above HashMap, your key will be a string and its value can be integer.

Application of ArrayList in Android studio.
As we told you, Arrays are very useful thing for a developer. Lets see use of ArrayList is Android studio.

Getting List Of Files in a directory

Let’s assume a condition, we have a directory in named as MyDir. Now we have to arrange the paths of all files so we can use in future. The code will be


ArrayList pathArray = new ArrayList();
File dir = new File(Environment.getExternalStorageDirectory()+"/MyDir");

File[] listFiles = dir.listFiles();

for(int i=0;i<listFiles.length; i++){
pathArray.add(listFiles[i].getAbsolutePath());
}

Now all the paths of file in directory are stored in pathArray and you can access them later. For example you can set them in a ListView for showing list of files in directory.

If you want to save in HashMap with name of file as key and its path as value. So that you can easily get path of a file by its name. You can store them in HashMap as follow.


ArrayMap<String,String> map = new ArrayMap<String,String>();

File dir = new File(Environment.getExternalStorageDirectory()+"/MyDir");

File[] listFiles = dir.listFiles();

for(int i=0;i<listFiles.length; i++){
String fileName = listFiles[i].getName();
String filePath = listFiles[i].getAbsolutePath();
map.put(fileName,filePath);
}

These were some use of ArrayList but there are a lot of more use of arrays in programming. As a beginner you must learn Arrays in Android studio. We hope you will like our tutorial. We may have missed something so if you find any error belong to above tutorial, please tell us in comment section. We will try to fix them soon.

Leave a Reply

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