1. Technology

Example Code for Working with Arrays


Java Code Listing:

The code below is example Java code for working with arrays. It shows how to declare an array from primitive data types, how that array can be manipulated using the Arrays class and how to use an ArrayList to create a dynamic array.

The articles that go with this code are Working with Arrays, Using the Arrays class and Using the Arraylist.

import java.util.Arrays;
import java.util.ArrayList;

public class ArrayExample {

    public static void main(String[] args) {
        //Declare an int array
        int[] intArray;
        //The int array cannot be used until it is intialized
        intArray = new int[10];
        //Add values to the elements in the array
        intArray[0] = 1;
        intArray[3] = 105;
        //The declaration and initialization can be done
        //on one line
        float[] floatArray = new float[10];
        //Multdimenstional arrays are just an array or arrays
        char[][] characterArray = {{'b','e','a','r'},
        char[][] duplicateCharacterArray = {{'b','e','a','r'},
         int[] numbersArray = {22,66,33,77,99,55,11,44,88};
         int[] duplicateNumbersArray = new int[9];
         //An array can be copied using the System.arraycopy method. Pass
         //the original array, the starting index, the destination array,
         //the starting index for it, and the length.
         System.arraycopy(numbersArray, 0, duplicateNumbersArray, 0, 9);
         //Using the equals operator creates a variable
         //that points to the same array in memory
         int[] sameArray = numbersArray;
         //This will be true as the both variables point to the same
         System.out.println("Does the numbersArray equal sameArray? " + numbersArray.equals(sameArray));
         //Even though the arrays hold exactly the same elements this 
         //will always be false as the equals method does not
         //look at the contents of the array.
         System.out.println("Does the numbersArray equal duplicateNumbersArray? " + numbersArray.equals(duplicateNumbersArray));
         //Use the Arrays.eequals method for a proper comparion of arrays
         System.out.println("Now does the numbersArray equal the duplicateNumbersArray? " + Arrays.equals(numbersArray, duplicateNumbersArray));
         //Use the Arrays.deepEquals for multidimensional arrays
         System.out.println("Are the multideminsonal arrays equal? " + Arrays.deepEquals(characterArray,duplicateCharacterArray));
         //Using the toString method is not very helpful for diaplying
         //the contents of the array:
         System.out.println("numbersArray.toString produces: " + numbersArray.toString());
         //Better to use Arrays.toString method
         System.out.println("Arrays.toString produces: " + Arrays.toString(numbersArray));
         //and Arrays.deepToString for multidimensional arrays:
         System.out.println("Arrays.deepToString produces: " + Arrays.deepToString(characterArray));
         //The Arrays.copyOf can be used to make a longer array 
         //from the original
         int[] longerNumbersArray = Arrays.copyOf(numbersArray, 20);
         System.out.println("New longer array elements: " + Arrays.toString(longerNumbersArray));
         //The Arrays.copyOf can be used to make a shorter array
         int[] shorterNumbersArray = Arrays.copyOf(numbersArray, 4);
         System.out.println("New shorter array elements: " + Arrays.toString(shorterNumbersArray));
         //The Arrays.copyOf can be used to take a selection of elements
         int[] rangedNumbersArray = Arrays.copyOfRange(numbersArray, 3,7);
         System.out.println("Selected array elements: " + Arrays.toString(rangedNumbersArray));
         //Sort the array in ascending order
         System.out.println("NumbersArray now sorted: " + Arrays.toString(numbersArray));
         //Try to find an element with the value 23
         System.out.println("Is there a 23? " + Arrays.binarySearch(numbersArray, 23));
         //Sort only a selection of elements
         System.out.println("Selective sort: " + Arrays.toString(duplicateNumbersArray));
         //Fill the array with zeros
         Arrays.fill(numbersArray, 0);
         System.out.println("All zeros now: " + Arrays.toString(numbersArray));
         //Fill a selection of elements
         Arrays.fill(duplicateNumbersArray, 3 ,duplicateNumbersArray.length, 0);
         System.out.println("Selective fill : " + Arrays.toString(duplicateNumbersArray));

         //Make an ArrayList from a String Array
         String[] names = {"Bob", "George", "Henry", "Declan", "Peter", "Steven"};
         ArrayList dynamicStringArray<String> = new ArrayList<String>(20);
         //Add a new elemnt at positon 3
         dynamicStringArray.add(3, "Max");
         //Remove the second element
         //Remove the element containing the String "Max"
         //Replace the element at posiont 3 with the String Paul
         //Show the contents of the ArrayList
         System.out.println("Contents of the dynamicStringArray: " + dynamicStringArray.toString());
         //Check the size of the ArrayList
         System.out.println("There are now " + dynamicStringArray.size() + " elements in the ArrayList");
         //Find the index of the element containing the String "George"
         System.out.println("The index position of George is : " + dynamicStringArray.indexOf("George"));
         //Clear all the elements in the ArrayList
         //Check to see if the ArrayList is empty
         System.out.println("Is the dynamicStringArray empty? " + dynamicStringArray.isEmpty());

  1. About.com
  2. Technology
  3. Java
  4. Java Syntax
  5. Understanding Data Types
  6. Example Code for Working with Arrays

©2014 About.com. All rights reserved.