Example usage for java.util Arrays fill

List of usage examples for java.util Arrays fill

Introduction

In this page you can find the example usage for java.util Arrays fill.

Prototype

public static void fill(Object[] a, int fromIndex, int toIndex, Object val) 

Source Link

Document

Assigns the specified Object reference to each element of the specified range of the specified array of Objects.

Usage

From source file:ArraysDemo.java

public static void main(String args[]) {
    int array[] = new int[10];
    for (int i = 0; i < 10; i++)
        array[i] = -3 * i;/*from w  ww.java2s  .  c om*/

    display(array);
    Arrays.sort(array);
    System.out.print("Sorted: ");
    display(array);

    // Fill and display the array.
    Arrays.fill(array, 2, 6, -1);
    System.out.print("After fill(): ");
    display(array);

    // Sort and display the array.
    Arrays.sort(array);
    System.out.print("After sorting again: ");
    display(array);

    // Binary search for -9.
    System.out.print("The value -9 is at location ");
    int index = Arrays.binarySearch(array, -9);

    System.out.println(index);
}

From source file:MainClass.java

public static void main(String args[]) {

    int array[] = new int[10];
    for (int i = 0; i < 10; i++)
        array[i] = 3 * i;/*  w  w w  .j  a v  a 2 s  .co m*/

    System.out.print("Original contents: ");
    display(array);
    Arrays.sort(array);
    System.out.print("Sorted: ");
    display(array);

    Arrays.fill(array, 2, 6, -1);
    System.out.print("After fill(): ");
    display(array);

    Arrays.sort(array);
    System.out.print("After sorting again: ");
    display(array);

    System.out.print("The value 9 is at location ");
    int index = Arrays.binarySearch(array, 9);

    System.out.println(index);
}

From source file:Person.java

public static void main(String[] a) {
    Person[] people = new Person[100];
    Arrays.fill(people, 0, 50, new Person("A", "B"));
    Arrays.fill(people, 50, 100, new Person("C", "D"));
    for (Person person : people) {
        System.out.println(person);
    }/*from  ww w.jav a 2 s.  c  om*/
}

From source file:Main.java

  public static void main(String[] argv) throws Exception {

  int startIndex = 0;
  int endIndex = 4;

  byte[] byteArr = new byte[10];
  byte byteFillValue = 1;

  Arrays.fill(byteArr, startIndex, endIndex, byteFillValue);
}

From source file:Main.java

  public static void main(String[] argv) throws Exception {

  int startIndex = 0;
  int endIndex = 4;

  char[] charArr = new char[10];
  char charFillValue = 1;

  Arrays.fill(charArr, startIndex, endIndex, charFillValue);
}

From source file:Main.java

  public static void main(String[] argv) throws Exception {

  int startIndex = 0;
  int endIndex = 4;

  short[] shortArr = new short[10];
  short shortFillValue = 1;

  Arrays.fill(shortArr, startIndex, endIndex, shortFillValue);

}

From source file:ArraysTester.java

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 {/*from ww w.j av a 2 s . com*/
        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.");
    }
}

From source file:Main.java

  public static void main(String[] argv) throws Exception {

  int startIndex = 0;
  int endIndex = 4;

  int[] intArr = new int[10];
  int intFillValue = 1;

  Arrays.fill(intArr, startIndex, endIndex, intFillValue);
}

From source file:Main.java

  public static void main(String[] argv) throws Exception {

  int startIndex = 0;
  int endIndex = 4;

  long[] longArr = new long[10];
  long longFillValue = 1;

  Arrays.fill(longArr, startIndex, endIndex, longFillValue);
}

From source file:Main.java

  public static void main(String[] argv) throws Exception {

  int startIndex = 0;
  int endIndex = 4;

  float[] floatArr = new float[10];
  float floatFillValue = 1;

  Arrays.fill(floatArr, startIndex, endIndex, floatFillValue);
}