Java Array Class
In Java array is a data structure, which can hold fixed size collection of same types of primitive data types or objects in sequence. Array in Java is an object, and part of java.util package. Many static methods are available for searching, sorting etc. Array elements storage in Java is index based, with first element at index 0.
The general form of one dimensional array declaration in Java is : variable_type variable_name[]; or variable_type[] variable_name; For example an integer array named as "iarray" for first five integers can be declared, instantiated and initialized as : int[] iarray = {1,2,3,4,5}; A string array named as "sarray" holding some alphabetical characters can be declared, instantiated, initialized as: String[] sarray = {"a", "b","c", "d", "e" };
An array can also be defined using new key word. An integer array of first five integers can be declared, instantiated and initialized as follow: int iarray[]=new int[5] // declaration and instantiation iarray[0]=1 // initialization iarray[1]=2; iarray[2]=3; iarray[3]=4; iarray[4]=5;
An array can store primitive data types and reference of object of any kind (not the object itself!). Array variable holds reference to array object in memory. Objects have to be created separately using the constructor of the corresponding class.
For multidimensional array, more number of square brackets need to be added for each additional dimension. A two dimensional array of integers for instance can be declared as: int[][] 2darray= {{1,2,3,4,5}, {6,7,8,9,10}}; Using the new key word, 2 dimensional array for five rows and five columns can be declared as: int[][] 2darray=new int[5][5]; Here first three elements of first row of two dimensional matrix can be written as : int[0][0] =1 ; // first row and first column int[0][1] =2 ; // first row and second column int[0][2] =2 ; // first row and third column
Java allows to create arrays with members of different sizes, known as Jagged arrays. A 2-D array, for instance can be of different number of columns in each row.
Java Array Questions
- What is difference between Array and ArrayList in Java?
- What is anonymous array? Give examples, when they are considered useful?
- How can you check the equality of two arrays in Java?
- Sort whole or part of a given array of integers and characters in ascending and descending order?
- Arrange a given array of strings in alphabetical order, based on first letter?
- What are different ways of searching an element of a given array?
- Find all the pairs in a given array, whose sum is equal to a given number?
- Find the first and second largest number in a given array of integers?
- Find a repeated number, if exist and number of occurences in a given array of integers?
- Declare an array of integers and add to find sum?
- Declare an array of integers and add to find sum of prime numbers?
- Declare two arrays of integers and find common numbers, if any?
- What does it mean to make an array volatile in java?
- Find missing number in a given array of integers?
- How to copy one array into another?