Learn Android — Custom Data Holders

The world’s most popular mobile OS — from phones and watches to cars and TVs.

Akshansh Dhing
Parsed Inc.
5 min readJun 13, 2018

--

Welcome to the Learn Android series. I’m going to share with you, regularly, why and how to start with Android and build your portfolio. I’ll share with you explanations and code snippets on how to implement the most important and basic things in Android.

In the previous article, we learned how to navigate from one activity to another and how different types of Intents are able to perform different types of tasks. We also learned how to pass data between activities through Intents. In this article, we’ll learn about different (custom) data holders that are used in Object Oriented Programming.

Previous Article: Learn Android — Multi-screen Applications

Arrays — are containers of fixed length. So once created, they stay of the same defined length. Arrays contain items of similar data types. For example, entire array of Strings, or integers, or boolean, etc. Each of the items are stored in ‘slots’ numbered from index ‘0’ to index defined ‘length-1’. And so when we need the items, we just use the index to display those values. For storing individual values, we can use variables. To learn more about the different data types, visit the documentation page here!

Arrays have a name, and each element is accessed using the name of the array and the index location. Index location is numbered from 0 → 1 → 2 → and so on till the end. So, for an array of index 8, we’ll have ‘slots’ numbered from 0 to 7.

For example, if we need to store integer heights of 9 students of a same class into the array we can define an array using —

Each element of array will contain one height which will be an integer. To explain the statement about, int is the datatype and [] denotes that we require an array. Then we name the array and using the ‘new’ keyword, we create an array of defined size, in our case — 7.

To initialize an element in an array we use the index and name —

Note that we do not use the datatype again since we’ve already defined it earlier. We also use the regular assignment operator ‘=’ along with the [] containing the index number.

To access or read the elements of the array, we use the similar syntax. For example, to print get the element at position 4 of the array, we use —

Now since we’ve learned about arrays, there’s actually some limitations about them. And Java supports a better data structure for us to store similar information — an ArrayList.

The main advantage is that we do not need to define a particular size as in an array. In an array once we define the size, we are stuck with it. But ArrayList is rather dynamic and allows adding and removal of elements and varies in size with respect to the amount of data present in it.

But the catch is that the ArrayList can only hold object data types as opposed to primitive data types — like integers, floats, boolean, etc. But if we want to store primitive types like double, Java supports “object grabbers”, which wrap around the primitive data types to convert them into object data types. For example, we can use the ArrayList<Double> but we cannot use ArrayList<double>.

So an ArrayList is more flexible since it doesn’t have wastage memory which maybe found in arrays when we define arrays larger than the required length.

Here’s a comparison chart between array and ArrayList —

Array vs. ArrayList

To learn about how to initialize, add or remove items, access items from an ArrayList’s please visit this tutorial!

Now since we’ve learned about data structures that can alone handle primitive operations that we need to perform, we need to know a way to write code which helps us do the same repetitive work over and over again, but without repeating and rewriting the code.

We do this using Loops.

Loops allow us to execute the same instructions over and over again until certain conditions are met and we exit the loop. So we can repeat the same set of instructions for every element of an Array or an ArrayList.

The while loop — will execute a set of instructions while the condition is ‘true’ and exits the loop as soon as the condition becomes ‘false’. Here’s how to write it down —

Compared to a while loop, the do-while loop will execute at least once because it checks the condition after the instructions are run. We can code the do-while loop like this —

For more information about the while and do-while loops, you can read the official Java documentation here!

The for loop — is used majorly to iterate over a range of values which repeatedly loops until a particular condition is satisfied. The for statement can be expressed as follows —

for (initialization; termination; increment)

Now, for example, if we want to print the height of all the students we added to the array, we can use a for loop like —

For more information about the for loop, you can read the official Java documentation here!

Next up: Learn Android — Lists and Adapters

Stay tuned for regular updates. Follow me and Parsed Inc. to never miss another one!

Also, let’s become friends on LinkedIn, GitHub, Twitter and Facebook!

To learn more about me and my work, visit my website!

Follow ParsedInc. on Facebook, LinkedIn, and Instagram!

If you enjoyed this article, feel free to 👏👏👏 a few times and share with a friend to help it reach someone who needs to read it. Thanks!

--

--