Here we will discuss One dimensional array in java and cover basic examples
What is One dimensional (Single dimensional) array in java
An array is a collection of similar elements. All array elements are stored in the contiguous memory location.
One dimensional or single-dimensional array is an array with one dimension.
One dimensional array declaration in java
Syntax
type arrayname[]; Or type [] array name;Example
int [] age;//collection of integer elements float[] price;// collocation of float elements; String [] names;// collocation of string element;In the above declaration, age is a reference variable that will hold reference of array same for price and for names.
One dimensional array construction
The array uses a
newoperator to dynamically allocate memory locations.
SyntaxArray name= new type [size];Example
age=new int[5]; price=new float[5]; names=new String[10];the new operator will dynamically allocate memory for 5 integer values that are 5*4=20 bytes continuously.
The array uses zero-based indexing where the first element is stored in a zero location. The last element is stored in the array size-1 location.
We can assign values in array elements one by one as below
age[0]=21; age[1]=43; age[2]=34; age[3]=37; age[4]=55;Single dimensional array initialization in java
Array declaration and initialization can be done in one statement
syntaxtype[] array name = {value,value,…..value};Example
int[] age= {21,43,34,37,55}Accessing array element: array element can be accessed by its index value.
Syntax
arrayName[indexExpression];For example age[0] access all first value of the array we can use a loop
We can print all values in age belowOne dimensional array in java examples
public class MyClass { public static void main(String args[]) { double[] price={22.33,44.22,55.66,33.66,77.11}; for(int i=0;i price[0]=22.33 price[1]=44.22 price[2]=55.66 price[3]=33.66 price[4]=77.11Array programs in java
public class FirstArray { public static void main(String[] s) { int[] age = new int[5]; age[0] = 21; age[1] = 23; age[2] = 45; age[3] = 31; age[4] = 33; for (int i = 0; i < 5; i++) { System.out.println("age[" + i + "]= " + age[i]); } } }Result
age[0]= 21 age[1]= 23 age[2]= 45 age[3]= 31 age[4]= 33We can also use for each loop to show array elements
Java array programs
public class JavaArray { public static void main(String[] s) { int[] age = new int[5]; age[0] = 21; age[1] = 23; age[2] = 45; age[3] = 31; age[4] = 33; for (int a : age) { System.out.println("age= " + a); } } }Output
age= 21 age= 23 age= 45 age= 31 age= 33Array input in java
The scanner class is used here to get Input in the array.
import java.util.Scanner; public class ArrayInputByUser { public static void main(String[] args) { int[] age = new int[5]; System.out.println("Enter age to save in array"); Scanner sc = new Scanner(System.in); for (int i = 0; i < 5; i++) { System.out.print("age[" + i + "]= "); age[i] = sc.nextInt(); } System.out.println("Age array values are"); for (int i = 0; i < 5; i++) { System.out.println("age[" + i + "]= " + age[i]); } } }Enter age to save in array age[0]= 4 age[1]= 5 age[2]= 6 age[3]= 7 age[4]= 4 Age array values are age[0]= 4 age[1]= 5 age[2]= 6 age[3]= 7 age[4]= 4Static array in java
To create a static array add the keyword static before an array.
public class StaticArrayExample { static final String[] EMAILS = {"[email protected]", "[email protected]", "[email protected]"}; public static void main(String[] args) { for (String s : EMAILS) { System.out.println(s); } } }[email protected] [email protected] [email protected]Length of array in java
the length property of an array is used to find the length of an array
public class MyClass { public static void main(String args[]) { String[] names={"Ram","Mohan","Sohan","Rita","Meeta","Geeta"}; System.out.println("Total names are "+names.length ); } }Total names are 6How to store string in array in java
public class MyClass { public static void main(String args[]) { String[] names = { "Ram", "Mohan", "Sohan", "Rita", "Meeta", "Geeta" }; System.out.println("Names are "); for (int i = 0; i < names.length; i++) { System.out.println(names[i]); } String[] branch = new String[5]; branch[0] = "Computer Science & Engineering"; branch[1] = "Mechanical Engineering"; branch[2] = "Civil Engineering"; branch[3] = "Electronics and Telecomm Engineering"; branch[4] = "Information Technology"; System.out.println("Branches are"); for (String s: branch) { System.out.println(s); } } }Names are Ram Mohan Sohan Rita Meeta Geeta Branches are Computer Science & Engineering Mechanical Engineering Civil Engineering Electronics and Telecomm Engineering Information TechnologyString to integer array in java
public class StringtoIntArray { public static void main(String[] args) { String s1 = "[1,2,3,4,5,6,7,8,9]"; System.out.println("String is "+s1); StringTokenizer s2 = new StringTokenizer(s1, "[,] "); int length = s2.countTokens(); int[] intArray = new int[length]; int i=0; while (s2.hasMoreTokens()) { intArray[i]=Integer.parseInt(s2.nextToken()); i++; } System.out.println("Integer Array is "); for (int j = 0; j < intArray.length; j++) { System.out.println("intArray["+j+"] = "+intArray[j]); } } }This program is taking a string as an argument converting it to StringTokenizer based on delimiter [,] and space.
A string tokenizer contains a set of string tokens these tokens are converted to an integer using Integer.parseInt().
above program can handle the following strings
String s1 = "1 2 3 4 5 6 7 8 9"; or
String s1 = "1,2,3,4,5,6,7,8,9";
String s1 = "[1,2,3,4,5,6,7,8,9]";
String is [1,2,3,4,5,6,7,8,9] Integer Array is intArray[0] = 1 intArray[1] = 2 intArray[2] = 3 intArray[3] = 4 intArray[4] = 5 intArray[5] = 6 intArray[6] = 7 intArray[7] = 8 intArray[8] = 9Anonymous array in java
anonymous arrays in java are arrays without a name
like
new Integer[]{1, 2, 3, 4, 5, 6, 7}
new String[]{"Ram","Mohan","Sohan"};
public class AnonymousArray { public static void printArray(Integer[] arr) { for (Integer a : arr) { System.out.print(" "+a); } } public static void main(String[] args) { AnonymousArray.printArray(new Integer[]{1, 2, 3, 4, 5, 6, 7}); } }