Learn Basics of Arrays tutorials with examples in java


Java Array Introduction

Java arrays examples

The array is used to hold of a collection of elements under a single name. Here a collection of elements are of the same type are passed. If you have a list of values to store it, without arrays, you have to declare elements, if there are 200 values, You have to declare 200 declarations

String  str1="one"  
String  str3="two"  
String  str3="three"  

Instead of a declaration of multiple values, Arrays store the collection of elements under a single variable name. Each element in an array can be accessed by using the index. Array index always start from zero and the maximum length is length -1

Arrays Features

Use length method to find out the length of arrays elements like an object.
Elements in the array are ordered, can be retrieved using index
Every element in an array implemented the cloneable and serializable interface
It creates dynamic memory
Array Syntax
Array Declaration syntax is as below

Datatype variableName[] or Datatype[]  variableName;  

Array Initialization syntax

variableName=new Datatype[size]  

size is required to create a space in memory during initialization.

How to create and initialize Array in java?

Arrays can be created in many ways. Using literals or using the new operator. The below example is about the creation of primitive array types

int[] array1 = new int[]{8,7,2,4 };   
int[] array2 = {8,7,2,4 };   
int array3[] = new int[5]  
  
String array creation and initialize  
String[] stringArray = new String[10];  
String[] stringArray1 = { "one", "two", "three", "four", "five" };  
String[] stringArray2 = new String[] { "one", "two", "three", "four", "five" };  

How to declare and initialize objects array?
An array of objects can be created same like primitive type five Employee object created and stored under array with the reference of employee objects

Employe[] list=new Employee[5]  
Employee{  
 String name;  
 public String getName() {  
  return name;  
 }  
 public void setName(String name) {  
  this.name = name;  
 }  
}  

Single and Two dimension array program example 

Single arrays are like normal array declaration and initialization

Integer [] ints=new Integer[5]  

The multidimensional array contains arrays of arrays Declare a Multi-dimension array

Integer twoDArrays[][]=new  Integer[2][2];  
twoDArrays[0][0]=1;  
twoDArrays[0][1]=2;  
twoDArrays[1][0]=2;  
twoDArrays[1][1]=3;  

How to create and initialize ArrayList from arrays? 

This is an example of a copy of array data to ArrayList. Arrays.asList method takes arrays as input and returns the collections, ArrayList constructor initialized with the collection and returns the ArrayList object.

String[] strs = { "one", "two", "three", "four", "five" };  
ArrayList listStrings = new ArrayList(Arrays.asList(strs));  
System.out.println(listStrings); // outputs [one, two, three, four, five]

How to find out if element/object exists in the array?

This is an example for finding an element in an array of elements or objects. First, convert the array to the collection using asList method. The collection has a method contains() return true if an element exists in a collection, else return false

String[] strs = { "one", "two", "three", "four", "five" };  
System.out.println(Arrays.asList(strs).contains("ade")); // outputs false  
System.out.println(Arrays.asList(strs).contains("one")); // outputs true  

How to convert Array to set in java with an example? 

Sometimes it is required to copy array data to set. Set does not allow duplicate elements. if arrays contain duplicate elements, copying array to set will not allow store duplicate elements. see below example for more information

String[] strs = { "one", "two", "three", "four", "five" ,"five"};  
Set set=new HashSet(Arrays.asList(strs));  
System.out.println(set); // outputs [four, one, two, three, five]

How to reverse an array of elements in java with the example?

  First, convert the array to collections using asList method. Then apply Collections.reverse method to reverse the collection

String[] strs = { "one", "two", "three", "four", "five" ,"five"};  
List listStrs = Arrays.asList(strs);  
System.out.println(listStrs); //outputs [one, two, three, four, five, five]  
Collections.reverse(listStrs);  
System.out.println(listStrs); // outputs [five, five, four, three, two, one]
Similar Posts