Java Arrays Class-Tutorial
Java Arrays Class Tutorial
Arrays class available in Java Collection Framework. This class belongs to java.util package. Arrays class provides various methods
such as sort(), asList(), binarySearch(), equals(), copyOf(), fill() etc., to work with arrays. These methods are helpful to manipulate the arrays efficiently.
All the methods of arrays class static methods and can be called directly with the class name without creating an object.ex: Arrays.<function Name>
such as sort(), asList(), binarySearch(), equals(), copyOf(), fill() etc., to work with arrays. These methods are helpful to manipulate the arrays efficiently.
All the methods of arrays class static methods and can be called directly with the class name without creating an object.ex: Arrays.<function Name>
Table of contents
- Sort Arrays - sort(),
- Convert Array to List - asList(arr)
- Search in Arrays - binarySearch()
- Compare two Arrays - equals()
- Create Copy of Arrays - copyOf(),
- Fill Array with Specific Value - fill()
Example: Sort Arrays
Syntax:
Arrays.sort(arrayname);
import java.util.Arrays;
public class Arraysdemo
{
public static void main(String[] args)
{
String array[]={"India", "Australia", "US", "Japan", "France"};
Arrays.sort(array);
for (String str: array)
{
System.out.println(str);
}
}
}
Arrays.sort- output
C:\javademo>java Arraysdemo
Australia
France
India
Japan
US
Example: Get List from Array.
Syntax:
Arrays.asList(arrayname);
import java.util.Arrays;
import java.util.List;
public class Arraysdemo
{
public static void main(String[] args)
{
String arr[]= new String[] { "Red", "Green", "Pink","Yellow"};
List<String> list = Arrays.asList(arr);
System.out.println("List: " + list);
}
}
Example: Search in Arrays
Syntax:
Arrays.binarySearch(arrayname, keyvalue);
import java.util.Arrays;
public class Arraysdemo
{
public static void main(String[] args)
{
int array[]={52,26,17,48,13,19,37,25,80};
int key = 13;
System.out.print(key+" found at index: "+Arrays.binarySearch(array,key));
}
}
Arrays.binarySearch - ouput:
C:\javademo>java Arraysdemo
13 found at index: 4
C:\javademo>
TopMenu
Example: Compare two Arrays
Syntax:
Arrays.equals(arrayone, arraytwo))import java.util.Arrays; public class Arraysdemo { public static void main(String[] args) { String array1[]={"Bangalore","Delhi", "Mumbai", "Kolkata"}; String array2[]={"Noida","Hyderabad", "Chennai", "Chandigarh"}; if(Arrays.equals(array1, array2)) { System.out.println("Arrays are equal"); } else { System.out.println("Arrays are not equal"); } } }
Arrays.equals - output
C:\javademo>java Arraysdemo
Arrays are not equal
C:\javademo>
MoveTop
Example: Create Copy of Arrays
Syntax:
Arrays.copyOf(array1, 4);
import java.util.Arrays; public class Arraysdemo { public static void main(String[] args) { String array1[]={"Delhi","Bangalore", "Mumbai", "Kolkata"}; String array2[]=Arrays.copyOf(array1, 4); System.out.println("Elements of Array 1"); for(String str:array1) { System.out.print(str+" "); } System.out.println("\n"); System.out.println("Elements of Array 2"); for(String str:array2) { System.out.print(str+" "); } } }
Arrays.copyOf - output
C:\javademo>java Arraysdemo
Elements of Array 2
Bangalore Delhi
C:\javademo>
Example: Fill Array with Specific Value
Syntax:
Arrays.fill(arrayname, char);
import java.util.Arrays;
public class Arraysdemo
{
public static void main(String[] args)
{
char array[]={'j','a' , 'v', 'a'};
Arrays.fill(array, '*');
for(char arr:array)
{
System.out.print(arr+" ");
}
}
}
Arrays.fill - output
C:\javademo>java Arraysdemo* * * *
C:\javademo>
Comments
Post a Comment