Lists 2 - Primitive array lists

Level 1 - Basics

Copy this fragment to the run() method of an exercise in IntelliJ.
Try to run it.

char[] letters = {'a', 'b', 'c', 'd', 'e', 'f' };

As you can see, not much is happening right now. This code creates (technically: declares and inializes) a list with characters. The list is called a primitive array. A primitive array has some similiarities with an ArrayList but there are also differences.

As a first assignment print the the first, middle and last character in the list.

char[] letters = {'a', 'b', 'c', 'd', 'e' }; char firstChar = letters[0]; char middleChar = letters[2]; char lastChar = letters[4]; SaxionApp.printLine( "First char:" + firstChar); SaxionApp.printLine( "Middle char:" + middleChar); SaxionApp.printLine( "Last char:" + lastChar);

As you can see, you can directly access the items in the list using the square bracket [ ] with the index of the item in the list. With the index also starting at zero. This example works well, since we now the number of items in the array.

To make it more dynamic and with the knowledge that you can retrieve the lenth of the array with the .length method, rewrite this code that it works for lists with any length (actually all lengths > 0).

char[] letters = {'a', 'b', 'c', 'd', 'e' }; int length = letters.length; char firstChar = letters[0]; char middleChar = letters[length / 2]; char lastChar = letters[length - 1]; SaxionApp.printLine( "First char:" + firstChar); SaxionApp.printLine( "Middle char:" + middleChar); SaxionApp.printLine( "Last char:" + lastChar);

Now copy the code below in the run() method of your project and run it.

char[] letters = new char[5];

// TODO: Add code here to fill the list with characters a,b,c,d,e

int length = letters.length;
char firstChar = letters[0];
char middleChar = letters[length / 2];
char lastChar = letters[length - 1];

SaxionApp.printLine( "First char:" + firstChar);
SaxionApp.printLine( "Middle char:" + middleChar);
SaxionApp.printLine( "Last char:" + lastChar);

You can see that alltough the programs does run, there are no characters printed. This is because the first line of the code is different and the array is only declared but not filled with values (in other words not initialized). It is up to you to initialize the array with the letters a,b,c,d and e. Do so, without changing the existing code.

char[] letters = new char[5]; letters[0] = 'a'; letters[1] = 'b'; letters[2] = 'c'; letters[3] = 'd'; letters[4] = 'e'; int length = letters.length; char firstChar = letters[0]; char middleChar = letters[length / 2]; char lastChar = letters[length - 1]; SaxionApp.printLine( "First char:" + firstChar); SaxionApp.printLine( "Middle char:" + middleChar); SaxionApp.printLine( "Last char:" + lastChar);

Once the array has been declared its size is fixed and cannot be changed afterwards. This is a big difference compared to an ArrayList, where that is perfectly possible. It is possible though to update values in an existing array:

letters[0] = 'z';

Please copy the code below in the run method and DO NOT run the code yet. What do you think is the output of the program?

    
char[] letters = new char[10];

letters[0] = 'a';
letters[1] = 'b';
letters[2] = 'c';
letters[3] = 'd';
letters[4] = 'e';

SaxionApp.printLine("The length of the array is:" + letters.length);

Do you predict 5 or 10? Well, run the code and confirm that the size is actually 10. So, even if you do not fill the individual array slots, it takes up the given size. Again, a difference compared to the ArrayList.

Write code that declares (not initializes) an array of type boolean with size 10 and print all array values with its indices. Indices being the position in the list.

boolean [] statuses = new boolean[10]; for (int i=0; i < statuses.length; i++) { SaxionApp.printLine("Status " + i + " is " + statuses[i]); }

Please note that, alltough the values have not been initialized the default value is false.

Change the code to find out what the default value for an int is?

int [] numbers = new int[10]; for (int i=0; i < numbers.length; i++) { SaxionApp.printLine("Number " + i + " is " + numbers[i]); }

So, now you know the default value for an int as well.

So, do you think it is possible to actually remove a value from the list, making the list automatically smaller? As you might have guessed, this is not possibile. Again, a major difference compared to the ArrayList. Using the .remove() method there actually reduces the size of the list and possible shifts other values as well.

Level 2 - Summary of the differences primitive array (list) and ArrayList

Please copy the code below to the run() method.

// Declare and initialize a primitive array with numbers
int[] arrayNumbers = {1, 2, 3, 4, 5};

// Create an arrayList with numbers
ArrayList listNumbers = new ArrayList<>();
for(int i = 1; i <= 5; i++) {
    listNumbers.add(i);
}

int arraySize = arrayNumbers.length; // Result is 5
int listSize = listNumbers.size(); // Result is 5

If you look at the code and run it. You will see that it behaves the same, this list have similar sizes and the content is also similar. But there are differences in the code between the two, can you see and tell?

  1. ArrayList uses Integer and primitive array int for data-types. So you can only use the capital variant of the datatype for ArrayList. That is: Boolean, Character, Integer, Double, String and so on. Where primitive array can also use: bool, char, int, double, String. (String being an exception.)
  2. ArrayList does not have initial size where primitive array does have that.
  3. Adding numbers to ArrayList is done via .add() method.
  4. Adding numbers to primitive array is done directly via initialization, like this int[] arrayNumbers = {1, 2, 3, 4, 5}; or later via arrayNumbers[2] = 10;
  5. The way of getting the sizes of the lists is different. .size() versus .length

Level 3 - Algorithms

Copy the code below in the run() method of your project.

int [] numbers = { 10, 5, 23, 48, 7 };
int smallest = numbers[0];
int largest = numbers[0];

for (int i=0; i < numbers.length; i++) {
    // TODO: find the smallest and largest number...

}
SaxionApp.printLine("The smallest number is:" + smallest);
SaxionApp.printLine("The largest number is:" + largest);

This code is not finished and does not give the correct results. From the array with numbers it should find its smallest and largest number. Update the code so that it does so. You only have to make additions inside the for-loop.

int [] numbers = { 10, 5, 23, 48, 7 }; int smallest = numbers[0]; int largest = numbers[0]; for (int i =0; i < numbers.length; i++) { // Find the smallest if (numbers[i] < smallest) { smallest = numbers[i]; } // Find the largest if (numbers[i] > largest) { largest = numbers[i]; } } SaxionApp.printLine("The smallest number is:" + smallest); SaxionApp.printLine("The largest number is:" + largest);