It's interesting, that on my quest to get my head around Java, there is real limited information on ArrayList and even Array's for that matter. I think this extends from the limitations that may be faced by the Array in Java, but it seems to be a major part of other programming languanges so I was surprised to see that it reletively glossed over by documentation. So, on that note, I hope this helps to provide some clarity.
So, one element lives in a house, just like a normal variable, while many elements live in an appartment building, just like an array.
In an array to add and reference a value you use the name of the array and the index element, just as you would an appartment. When we create the array, instead of specifying the type of variable, we specify type[], with squarte brackets so Java knows we are creating an array.
For example, if we wanted to create a String array with 5 elements, we would write:
String[] list = new String[5];
Keep in mind that the values of our array is numbered from 0 to 4, so if we wanted to access the value of the third element, we would write:
System.out.println(list[2]);
To print to screen the number of elements in the array you would use the following function:
System.out.println(list.length);
To assign a value to an element, you can do the following:
list[1] = "Cat";
Summary of Arrays:
1 import java.io.*;
2
3 public class Arrays {
4
5 public static void main(String[] args) throws Exception {
6
7 Reader r = new InputStreamReader(System.in);
8 BufferedReader reader = new BufferedReader(r);
9
10 //Create the array and read in the values from the keyboard
11 String[] list = new String[10];
12 for(int i = 0; i < list.length; i++){
13 String s = reader.readLine();
14 list[i] = s;
15 }
16
17 //List all the values in the array
18 for(int i = 0; i < list.length; i++){
19 System.out.println(list[i]);
20 }
21 }
22 }
This is where the ArrayList class comes in as it can allow us to create an array that can change its size. Things are pretty similar to creating and working with arrays but it allows us to do some things that we couldn't do previously. This includes support for inserting and eleting elements from the middle of the array without leaving holes. An ArrayList can also change the size of the array.
To create an ArrayList, we no do this:
ArrayList<String> list = new ArrayList<String>();
To get the number of elements in the ArrayList, we can do this:
System.out.println(list.size());
To get the value of a specific element we can do the following:
System.out.println(list.get(3));
If we want to set the third element as the value of "s", we could then do:
list.set(3, s);
1 import java.util.*;
2 import java.io.*;
3
4 public class Arrays {
5
6 public static void main(String[] args) throws Exception {
7
8 Reader r = new InputStreamReader(System.in);
9 BufferedReader reader = new BufferedReader(r);
10
11 //Create the arraylist and read in the values from the keyboard
12 ArrayList<String> list = new ArrayList<String>();
13 for(int i = 0; i < 10; i++){
14 String s = reader.readLine();
15 list.add(s);
16 }
17
18 //List all the values in the array
19 for(int i = 0; i < list.size(); i++){
20 System.out.println(list.get(i));
21 }
22 }
23 }
You need to make sure the java.util.* library is imported at the start of our code to make sure the ArrayList class is available for use to use.