Arrays: fill(int[] a,int fromIndex,int toIndex,int val) : Arrays « java.util « Java by API






Arrays: fill(int[] a,int fromIndex,int toIndex,int val)

   
/**
 *Output: 
Original contents: 0 3 6 9 12 15 18 21 24 27 
Sorted: 0 3 6 9 12 15 18 21 24 27 
After fill(): 0 3 -1 -1 -1 -1 18 21 24 27 
After sorting again: -1 -1 -1 -1 0 3 18 21 24 27 
The value 9 is at location -7
 */

import java.util.Arrays;

public class MainClass {
  public static void main(String args[]) {

    int array[] = new int[10];
    for (int i = 0; i < 10; i++)
      array[i] = 3 * i;

    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);
  }

  static void display(int array[]) {
    for (int i : array) {
      System.out.print(i + " ");
    }
    System.out.println();
  }
}

           
         
    
    
  








Related examples in the same category

1.Arrays: asList(T[] a)
2.Arrays: binarySearch(int[] a,int key)
3.Arrays: binarySearch(Object[] a, Object key)
4.Arrays: copyOf(String[] original, int newLength)
5.Arrays: copyOfRange(String[] original, int from, int to)
6.Arrays: deepToString(Object[] a)
7.Arrays: equals(boolean[] a, boolean[] a2)
8.Arrays: equals(char[] a, char[] a2)
9.Arrays: equals(double[] a, double[] a2)
10.Arrays: equals(float[] a, float[] a2)
11.Arrays: equals(int[] a, int[] a2)
12.Arrays: equals(long[] a, long[] a2)
13.Arrays: equals(Object[] a, Object[] a2)
14.Arrays: equals(short[] a, short[] a2)
15.Arrays: fill(boolean[] a, boolean val)
16.Arrays: fill(boolean[] a, int fromIndex, int toIndex, boolean val)
17.Arrays: fill(byte[] a, byte val)
18.Arrays: fill(byte[] a, int fromIndex, int toIndex, byte val)
19.Arrays: fill(char[] a, char val)
20.Arrays: fill(char[] a, int fromIndex, int toIndex, char val)
21.Arrays: fill(double[] a, double val)
22.Arrays: fill(double[] a, int fromIndex, int toIndex, double val)
23.Arrays: fill(float[] a, float val)
24.Arrays: fill(float[] a, int fromIndex, int toIndex, float val)
25.Arrays: fill(int[] a, int val)
26.Arrays: fill(long[] a, long val)
27.Arrays: fill(long[] a, int fromIndex, int toIndex, long val)
28.Arrays: fill(Object[] a, Object val)
29.Arrays: fill(Object[] a, int fromIndex, int toIndex, Object val)
30.Arrays: fill(short[] a, short val)
31.Arrays: fill(short[] a, int fromIndex, int toIndex, short val)
32.Arrays: sort(byte[] a)
33.Arrays: sort(byte[] a, int fromIndex, int toIndex)
34.Arrays: sort(char[] a)
35.Arrays: sort(char[] a, int fromIndex, int toIndex)
36.Arrays: sort(double[] a)
37.Arrays: sort(double[] a, int fromIndex, int toIndex)
38.Arrays: sort(float[] a)
39.Arrays: sort(float[] a, int fromIndex, int toIndex)
40.Arrays: sort(in[] a)
41.Arrays: sort(T[] a, Comparator c)
42.Arrays: toString(Object[] a)