This segment will teach you how you can store more than one value in a single variable. This is done using “Arrays”. You will now learn how to declare these arrays and how to use them.
So till now you have played around a little with different kinds of data types, seen some arithmetic operations, casting and also seen how strings and characters work. Now let us move on to something different. Suppose that I have the list of all the students in my class who are enrolled in a particular course. So for example, I have these four students us. Now, if I wish to store the names of these four students, one way to do this would be to store them in different variables. So I would say string s one equals to Adam, s two is Lucy, s three is Emma and s four is John. So now, if I would find a way to accumulate all these four names and store them in a single collection of these strings, it would be far more beneficial. And why would it be beneficial? Well, if I could collect all these four names at one place and store them inside a single object, then it would make my life definitely easier because then I would not have to remember all these variable names. So basically, earlier we read that these variables are nothing but different small containers which contains these values which are the names of these students. Now, if somehow I could store these four small containers inside a big container, then it would make my life easier, because then I would not need to remember the names of these four small containers, and I could simply assign a single name to this particular big container and then try to retrieve the names of the students from here. So basically, arrays is one data type which help us to accumulate different kinds of objects all at one place. The advantage of arrays is that if I have say, a list of 100 students, then I don't need to declare 100 variables. For this I can simply declare just one variable. That variable will be my array. So basically another analogy which we can think of is that suppose a teacher comes into the classroom and the teacher wishes to take the attendance role of the students. So wouldn't it be better if the teacher would have the list of all the hundred students in the class written down on one single page of the notebook? Then the teacher could simply move from one name to another, role number wise, and take the attendance. So basically, an array is nothing but a list of objects which are made of the same data type. That is, I can declare an array which would store strings inside it, or I can declare an array which would store numbers inside it. Suppose I'm conducting a scientific experiment and I get some readings. Then I can also declare an array which would store those readings inside that particular array. So in cases such as scientific experiments, there might be hundreds and thousands of values. So instead of declaring 1000 different variables, I can simply store them all at one place in my array collection. So here basically in my array, I can construct four boxes like this and store these four strings inside it. I can assign values to all these four boxes and I can retrieve their values by using an internal numbering system. So all the arrays in Java have an internal numbering system. For example, if I have four elements inside my array, then the numbering will begin from zero, will increase by one, two and three. So basically, if I have an array of four objects, the numbering which in Java we call as the indexing will always begin from zero and will keep on getting incremented one by one. Another important thing to notice is that for a single array it can only contain different objects which belong to the same data type. For example, if this object is a string, then all the objects inside this array should all be strings. Otherwise it would throw up an error. Or if this array consists of numbers only, then all of these objects must all be numbers. So now let us learn how you can declare a new array and probably assign values to them. So, for declaring an array, I would say can we first think of a name for our array? Suppose this is my big container here in which I wish to store these four names. Now suppose I wish to call this particular container students.
So if I want my array to be called as students and I wish that only there should be strings inside this array, I would say string. So this basically tells the computer that I am about to assign string values to this array. Then I say students. This denotes the name of the array. And another careful thing is that after students make these two square brackets so this statement basically string students followed by opening and closed square bracket denotes that this variable, students is actually an array only of strings.
Introduction to data types, arithmetic operations, casting, strings, and characters
The benefits of storing multiple objects in a single collection using arrays
Arrays allow for easier management and organization of data, particularly with large sets of objects
Arrays are lists of objects with the same data type, such as strings or numbers
The internal numbering system for arrays begins at zero and increases by one for each element
Arrays can only contain objects of the same data type
Declaration of a new array involves choosing a name and specifying the data type and brackets to denote an array
Example of an array for storing student names as strings is provided.
Note that all the elements in an array can only be of one data type. i.e if an array is declared as String, only string type data can be stored at each index and no other data type is compatible.
Now that you know how to declare an array of a particular data type. In the following video, you will learn about the process of storing data in an array.
You have learned how you can use arrays to store multiple values in a variable. You write the data type first, followed by the name of the array, followed by square brackets.
To initialize an array, you can do this:
<array name> = new <data type>[<number of elements in the array>];
For example, names=new String[4];
Also, each element in an array can be accessed using its index. The index starts from 0 and not 1; this is something important that you need to remember. You will find arrays very useful when you reach the Functions module.
So, to store data at each index, you can do this;
<array name>[<index>]=<data>;
For example; names[1]=”Aishwarya”;
There is another way to declare and initialize an array and store elements in it. Let us take a look at this simpler and faster method.
As you already saw, there is another way to declare and initialize an array in the same line.
For example:
String names[]={“Ankit”,”Aishwarya”,”Ifrah”}
Let us now try to run all the new statements you learned about and observe the results.
You can download the following java file which will be very useful to you while watching the video. Right click on the java file and open it with Intellij. Please be careful with the name of the java file. The name of the java file should exactly be same as the class name inside it for the code to run. So make changes accordingly.
In the above video, you saw how to declare and initialize arrays. Also, you saw what happens when you try to access an index in an array which does not exist within bounds of the array.
Till now, you have learned to store and manipulate data which is in the form of numbers or characters.
In the next segment, you will learn about operations done on boolean variables which store only true/false values.