Static methods from Arrays : Arrays « Collections Data Structure « Java






Static methods from Arrays

Static methods from Arrays
 
/*
License for Java 1.5 'Tiger': A Developer's Notebook
     (O'Reilly) example package

Java 1.5 'Tiger': A Developer's Notebook (O'Reilly) 
by Brett McLaughlin and David Flanagan.
ISBN: 0-596-00738-8

You can use the examples and the source code any way you want, but
please include a reference to where it comes from if you use it in
your own products or services. Also note that this software is
provided by the author "as is", with no expressed or implied warranties. 
In no event shall the author be liable for any direct or indirect
damages arising in any way out of the use of this software.
*/

import java.util.Arrays;
import java.util.List;

public class ArraysTester {

  private int[] ar;

  public ArraysTester(int numValues) {
    ar = new int[numValues];

    for (int i=0; i < ar.length; i++) {
      ar[i] = (1000 - (300 + i));
    }
  }

  public int[] get() {
    return ar;
  }

  public static void main(String[] args) {
    ArraysTester tester = new ArraysTester(50);
    int[] myArray = tester.get();

    // Compare two arrays
    int[] myOtherArray = tester.get().clone();
    if (Arrays.equals(myArray, myOtherArray)) {
      System.out.println("The two arrays are equal!");
    } else {
      System.out.println("The two arrays are not equal!");
    }

    // Fill up some values
    Arrays.fill(myOtherArray, 2, 10, new Double(Math.PI).intValue());
    myArray[30] = 98;

    // Print array, as is
    System.out.println("Here's the unsorted array...");
    System.out.println(Arrays.toString(myArray));
    System.out.println();

    // Sort the array
    Arrays.sort(myArray);
    
    // print array, sorted
    System.out.println("Here's the sorted array...");
    System.out.println(Arrays.toString(myArray));
    System.out.println();

    // Get the index of a particular value
    int index = Arrays.binarySearch(myArray, 98);
    System.out.println("98 is located in the array at index " + index);

    String[][] ticTacToe = { {"X", "O", "O"},
                             {"O", "X", "X"}, 
                             {"X", "O", "X"}};
    System.out.println(Arrays.deepToString(ticTacToe));

    String[][] ticTacToe2 = { {"O", "O", "X"},
                              {"O", "X", "X"}, 
                              {"X", "O", "X"}};

    String[][] ticTacToe3 = { {"X", "O", "O"},
                              {"O", "X", "X"}, 
                              {"X", "O", "X"}};

    if (Arrays.deepEquals(ticTacToe, ticTacToe2)) {
      System.out.println("Boards 1 and 2 are equal.");
    } else {
      System.out.println("Boards 1 and 2 are not equal.");
    }

    if (Arrays.deepEquals(ticTacToe, ticTacToe3)) {
      System.out.println("Boards 1 and 3 are equal.");
    } else {
      System.out.println("Boards 1 and 3 are not equal.");
    }
  }
}
           
         
  








Related examples in the same category

1.illustrates how to use some of the methods of the Arrays class:
2.Sorting, Searching, and Inserting into a sorted arraySorting, Searching, and Inserting into a sorted array
3.Demonstrate use of Arrays.sort on Booleans
4.Convert an Array to a Vector
5.Convert Array to Collection
6.Convert array of object to array of primitive?
7.Compare if two array is equal
8.Array Search Test
9.Array Fill Test Array Fill Test
10.Copy some items of an array into another array
11.Sort an arraySort an array
12.Sort an array: case-sensitive
13.Sort an array: case-insensitive
14.Java Sort byte Array
15.Java Sort char Array
16.Java Sort double Array
17.Java Sort float Array
18.Expanding an Array
19.Compare two byte type arrays
20.Compare two char type arrays
21.Compare two short type arrays
22.Compare two int type arrays
23.Compare two long type arrays
24.Compare two float type arrays
25.Compare two double type arrays
26.Filling Elements in an Array: boolean type
27.Filling Elements in an Array: byte type
28.Filling Elements in an Array: char type
29.Filling Elements in an Array: short type
30.Filling Elements in an Array: int type
31.Filling Elements in an Array: long type
32.Filling Elements in an Array: float type
33.Filling Elements in an Array: double type
34.filling object arrays:
35.fill to a contiguous range of elements in an array: boolean array
36.Fill to a contiguous range of elements in an array: byte array
37.Fill to a contiguous range of elements in an array: char array
38.Fill to a contiguous range of elements in an array: short array
39.Fill to a contiguous range of elements in an array: int array
40.Fill to a contiguous range of elements in an array: long array
41.Fill to a contiguous range of elements in an array: float array
42.Use Arrays.asList() to convert Array to List
43.Merge (or add) two arrays into one
44.Fill to a contiguous range of elements in an array: double array
45.Fill to a contiguous range of elements in an array: String array
46.Minimum and maximum number in array
47.Finding an Element in a Sorted Array
48.Shuffle elements of an array
49.Shifting Elements in an Array: Shift all elements right by one
50.Shifting Elements in an Array: Shift all elements left by one
51.Use java.util.Arrays.deepToString() to dump the multi-dimensional arrays
52.use Arrays.copyOf to copy array
53.If the element is a primitive type.
54.When comparing Object arrays, null elements are equal. If the elements are not null, Object.equals() is used.
55.Comparing Arrays: null arrays are equal
56.Compare two boolean arrays and null