Java Collection Framework - Arrays

Java Collection Framework - Arrays


Posted in : Core Java Posted on : May 28, 2011 at 4:52 PM Comments : [ 0 ]

In Java Arrays is a class which extends the class Object. The methods existing in this class are static for performing the operations like searching, sorting, comparing, and filling array elements. This class is a part of Java Collection Framework and is available in java.util package.

Java Collection Framework - Arrays

In Java Arrays is a class which extends the class Object. The methods existing in this class are static for performing the operations like searching, sorting, comparing, and filling array elements. This class is a part of Java Collection Framework and is available in java.util package.

Syntax

public class Arrays extends Object

Methods of Arrays

This class provides various methods respecting to the performing operations  some of then are as follows :

  • asList() 

                              syntax : public static <T> List<T> asList(T... a)

  • binarySearch() :  This method has various different parameters for different operations.

                              syntax : public static int binarySearch(byte[] a, byte key)

  • copyOf() :            This method has various different parameters for different operations.

                              syntax : public static boolean[] copyOf(boolean[] original, int newLength)

  • copyOfRange() : This method has various different parameters for different operations.

                             syntax : public static boolean[] copyOfRange(boolean[] original, int from, int to)

  • equals() :          This method has various different parameters for different operations.

                            syntax : public static boolean equals(boolean[] a, boolean[] a2)

  • fill() :                This method has various different parameters for different operations.

                           syntax : public static void fill(boolean[] a, boolean val)

  • hashCode() :   This method has various different parameters for different operations.

                           syntax : public static int hashCode(boolean[] a)

  • sort() :             This method has various different parameters for different operations.

                           syntax : public static void sort(byte[] a)

  • toString() :     This method has various different parameter for different operations.

                          syntax : public static String toString(boolean[] a

Example :

package devmanuals.com;

import java.util.Arrays;

import java.util.Collections;

import java.util.List;

public class ArraysDemo {

  public static void main(String[] args) {

    String[] a = "Mango""Apple""PineApple""Grapes" };

    List l = Arrays.asList(a);

    System.out.println("Unsorted list = "+l);

    Collections.sort(l);

    System.out.println("Sorted list = "+l);

    int i = Arrays.binarySearch(a, "PineApple");

    System.out.println("In array PineApple is at the position : " + i);

  }
}

Output :

Unsorted list = [Mango, Apple, PineApple, Grapes]

Sorted list = [Apple, Grapes, Mango, PineApple]

In array PineApple is at the position : 3

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics