Title: ArrayList
Course: CPS 261: Advanced Java Concepts ~ DL & MM MASTER

Array Class

Depending on what your primary operations are, you might choose a datastructure that is the most efficient for the normal operations that you encounter. Every datastructure has its own advantages and disadvantages. Java provides a wide family of datastructures. Each has its own advantages and disadvantages.

Before we move into the Data Structures that Java provides (i.e. ArrayList, LinkedList, TreeSet, etc.) we need to look at some of the characteristics of Arrays and some of the features provided from java.util.Arrays.

1. Arrays are efficient if they meet the needs of your application.
2. You can store Classes, or primitives(int, long, double, etc.) in arrays.
3. You have to know how many elements that are required up front. Otherwise, you end up writing code to create a larger array and copy the previous array into the new bigger array.
4. Java provides the class java.util.Arrays which provides many useful static methods such as:
  - toString
  - sort
  - binarySearch
  - fill
  - equals
  - copyOf
  - copyOfRange
5. Check out the documentation and note that these methods + others are available for a wide range of Java types.
6. In this video we will first illustrate using these methods for Strings. Strings have everything working for them that you need to make these methods work well.
7. Next we will take a look at using a primitive (int). With a small number of changes, everything works.
8. Lastly we will take a Dog class and see what happens when we try this out.

TryOutArrays.java

// This code is developed iteratively in the video


package arrays;

import java.util.Arrays;

class Dog implements Comparable
{
    String name;
    int age;
    Dog(String name, int age)
    {
        this.name = name;
        this.age = age;
    }
    public String toString()
    {
        return name +":"+ age;
    }
    @Override
    public int compareTo(Object other) {
        Dog d = (Dog)other;
        int retval= name.compareTo(d.name);
        if (retval == 0)
        {
            retval = age - d.age;
        }
        return retval;

    }
    public boolean equals(Object ob)
    {
        if (ob == null)
            return false;
        Dog d = (Dog)ob;
        if (name.equals(d.name) && age == d.age)
            return true;
        else
            return false;
    }
}
public class TryOutArrays {

    static void tryInts()
    {
        int[] arr = {5, 3, 2, 1, 6, 4};
        int [] otherArr = new int[4];

        String temp= Arrays.toString(arr);
        System.out.println("original=" +temp);

        Arrays.sort(arr);
        temp = Arrays.toString(arr);
        System.out.println("sorted=" + temp);

        int index = Arrays.binarySearch(arr,  3);
        System.out.println("index of gamma = " + index);
        index = Arrays.binarySearch(arr,  7);
        System.out.println("index of rho = " + index);

        Arrays.fill(otherArr, 9);
        temp = Arrays.toString(otherArr);
        System.out.println("filled otherArr= " +temp);

        otherArr = Arrays.copyOf(arr, 10);
        temp = Arrays.toString(otherArr);
        System.out.println("copied otherArr expanded= " +temp);

        otherArr = Arrays.copyOf(arr, 5);
        temp = Arrays.toString(otherArr);
        System.out.println("copied otherArr truncated= " +temp);

        otherArr = Arrays.copyOfRange(arr, 2 , 10);
        temp = Arrays.toString(otherArr);
        System.out.println("copyOfRange otherArr subrange= " +temp);

        if (!Arrays.equals(arr, otherArr))
            System.out.println("arr is NOT the same as otherArr");

        otherArr = Arrays.copyOf(arr, 6);
        if (Arrays.equals(arr, otherArr))
            System.out.println("arr is the same as otherArr");
    }

    static void tryStrings()
    {
        String[] arr = {"zeta", "gamma", "pi", "epsilon", "alpha", "beta"};
        String [] otherArr = new String[4];

        String temp= Arrays.toString(arr);
        System.out.println("original=" +temp);

        Arrays.sort(arr);
        temp = Arrays.toString(arr);
        System.out.println("sorted=" + temp);

        int index = Arrays.binarySearch(arr,  "gamma");
        System.out.println("index of gamma = " + index);
        index = Arrays.binarySearch(arr,  "rho");
        System.out.println("index of rho = " + index);

        Arrays.fill(otherArr, "blank");
        temp = Arrays.toString(otherArr);
        System.out.println("filled otherArr= " +temp);

        otherArr = Arrays.copyOf(arr, 10);
        temp = Arrays.toString(otherArr);
        System.out.println("copied otherArr expanded= " +temp);

        otherArr = Arrays.copyOf(arr, 5);
        temp = Arrays.toString(otherArr);
        System.out.println("copied otherArr truncated= " +temp);

        otherArr = Arrays.copyOfRange(arr, 2 , 10);
        temp = Arrays.toString(otherArr);
        System.out.println("copyOfRange otherArr subrange= " +temp);

        if (!Arrays.equals(arr, otherArr))
            System.out.println("arr is NOT the same as otherArr");

        otherArr = Arrays.copyOf(arr, 6);
        if (Arrays.equals(arr, otherArr))
            System.out.println("arr is the same as otherArr");
    }

    static void tryDogs()
    {

        Dog[] arr = {new Dog("zeta",1),  new Dog("gamma",3), new Dog("pi",6),
                new Dog("epsilon",4), new Dog("alpha", 5), new Dog("beta",2)};
        Dog [] otherArr = new Dog[4];

        String temp= Arrays.toString(arr);
        System.out.println("original=" +temp);

        Arrays.sort(arr);
        temp = Arrays.toString(arr);
        System.out.println("sorted=" + temp);

        int index = Arrays.binarySearch(arr,  new Dog("gamma",5));
        System.out.println("index of gamma = " + index);
        index = Arrays.binarySearch(arr,  new Dog("rho",4));
        System.out.println("index of rho = " + index);

        Arrays.fill(otherArr, new Dog("Spike",4));
        temp = Arrays.toString(otherArr);
        System.out.println("filled otherArr= " +temp);

        otherArr = Arrays.copyOf(arr, 10);
        temp = Arrays.toString(otherArr);
        System.out.println("copied otherArr expanded= " +temp);

        otherArr = Arrays.copyOf(arr, 5);
        temp = Arrays.toString(otherArr);
        System.out.println("copied otherArr truncated= " +temp);

        otherArr = Arrays.copyOfRange(arr, 2 , 10);
        temp = Arrays.toString(otherArr);
        System.out.println("copyOfRange otherArr subrange= " +temp);

        if (!Arrays.equals(arr, otherArr))
            System.out.println("arr is NOT the same as otherArr");

        otherArr = Arrays.copyOf(arr, 6);
        otherArr[0] = new Dog(otherArr[0].name, otherArr[0].age);
        if (Arrays.equals(arr, otherArr))
            System.out.println("arr is the same as otherArr");
    }


    public static void main(String[] args)
    {

        System.out.println("************** Strings ******************");
        tryStrings();
        System.out.println("************** ints ******************");
        tryInts();
        System.out.println("************** Dogs ******************");
        tryDogs();



    }

}



Last Updated: October 22, 2013 12:54 PM