Arrays in java

Arrays in Java

  • Java provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type.
  • An array is used to store a collection of data, but it is often more useful to think of and array as a collection of variables of the same type.

 

Note:

  • Array is collection of element.
  • Array hold element of same type.
  • Array is fixed in size.

How to declare array in java

[pastacode lang=”java” manual=”package%20array123%3B%0A%0Apublic%20class%20IntegerArrays%20%0A%7B%0A%09public%20static%20void%20main(String%5B%5D%20args)%20%0A%09%7B%0A%09%20%20%20%20%20int%20a%5B%5D%3D%7B1%2C2%2C3%2C4%2C6%2C72%7D%3B%2F%2Fdeclaring%20Array%0A%09%09%0A%09%09System.out.println(%22Array%20elements%22)%0A%09%09%0A%09%09for(int%20i%3D0%3Bi%3Ca.length%3Bi%2B%2B)%0A%09%09%7B%0A%09%09%09System.out.println(a%5Bi%5D)%3B%09%0A%09%09%7D%09%0A%09%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:
Array elements
1
2
3
4
6
72

Another Way to declare Arrays

Syntax: Datatype variableName[] = new DataType[Size];

It is necessary to give size of Array if you not give then it will give an error.

[pastacode lang=”java” manual=”package%20array123%3B%0A%0Apublic%20class%20ArrayExample%20%0A%7B%0A%09public%20static%20void%20main(String%5B%5D%20args)%20%0A%09%7B%09%0A%09%09int%20a%5B%5D%3Dnew%20int%5B5%5D%3B%2F%2Fdeclaring%20array%20of%20size%205%09%0A%09%09%0A%09%09%2F%2Fassigning%20value%20to%20array%0A%09%09%0A%09%09a%5B0%5D%3D23%3B%0A%09%09a%5B1%5D%3D24%3B%0A%09%09a%5B2%5D%3D11%3B%0A%09%09a%5B3%5D%3D54%3B%0A%09%09a%5B4%5D%3D43%3B%0A%09%09%09%0A%09%09%2F%2Fretrieving%20element%20using%20for%20loop%0A%09%09for(int%20i%3D0%3Bi%3Ca.length%3Bi%2B%2B)%0A%09%09%7B%0A%09%09System.out.println(a%5Bi%5D)%3B%09%0A%09%09%7D%0A%09%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:
23
24
11
54
43

Program to Create Dynamic Arrays

[pastacode lang=”java” manual=”package%20PRoglacLogic%3B%0Aimport%20java.util.Scanner%3B%0A%0Apublic%20class%20DynamicARray%0A%20%7B%09%0A%09public%20static%20void%20main(String%5B%5D%20args)%20%0A%09%7B%0A%09%09Scanner%20c%3Dnew%20Scanner(System.in)%3B%0A%09%09%0A%09%09System.out.println(%22enter%20howmany%20element%20u%20want%20to%20enter%22)%3B%0A%09%20%20%20%20%20%20%20%20int%20a%3Dc.nextInt()%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%0A%09%09int%20arr1%5B%5D%3Dnew%20int%5Ba%5D%3B%0A%09%09%0A%09%09for(int%20i%3D0%3Bi%3Carr1.length%3Bi%2B%2B)%0A%09%09%7B%0A%09%09%09arr1%5Bi%5D%3Dc.nextInt()%3B%0A%09%09%7D%0A%09%09%0A%09%09System.out.println(%22element%20are%22)%3B%0A%09%09%0A%09%09for(int%20i%3D0%3Bi%3Carr1.length%3Bi%2B%2B)%0A%09%09%7B%0A%09%09%09System.out.println(arr1%5Bi%5D)%3B%0A%09%09%7D%0A%09%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:
enter howmany element u want to enter
3
12
13
14
element are
12
13
14
How to declare 1D,2D and 3D array

[pastacode lang=”java” manual=”package%20array%3B%0A%0Apublic%20class%20Arraylength%20%7B%0A%09%0A%09public%20static%20void%20main(String%5B%5D%20args)%20%0A%09%7B%09%20%0A%09%20int%5B%5D%20arr1%3Dnew%20int%5B5%5D%3B%20%20%20%20%20%2F%2F%201D%20array%0A%09%09%20%0A%09%20int%5B%5D%5B%5D%20arr2%3Dnew%20int%5B3%5D%5B4%5D%3B%20%20%20%20%2F%2F%202D%20array%0A%09%09%09%09%0A%09%20int%5B%5D%5B%5D%5B%5D%20arr3%3D%20new%20int%5B2%5D%5B3%5D%5B5%5D%3B%20%20%20%20%2F%2F%203d%20Array%0A%09%09%20%0A%09%20%2F%2F%201D%20array%0A%09%20System.out.println(%22array%20length%20of%201d%20array%3D%20%22%2Barr1.length)%3B%0A%09%09%20%0A%09%20%2F%2F%202%20D%20array%0A%09%20System.out.println(%222%20d%20%20array%20length%3D%22%2Barr2.length)%3B%2F%2Fnumber%20of%20row%0A%09%20System.out.println(%22in%202%20d%20array%20number%20of%20column%3D%22%2Barr2%5B1%5D.length)%3B%0A%09%09%20%0A%09%20%2F%2F%203%20D%20array%0A%09%09%20%0A%09%20System.out.println(%223d%20array%20length%3D%22%2Barr3.length)%3B%0A%09%20System.out.println(%223d%20array%20length%3D%22%2Barr3%5B0%5D.length)%3B%0A%09%20System.out.println(%223d%20array%20length%3D%22%2Barr3%5B1%5D.length)%3B%0A%09%09%20%0A%09%09%20%0A%09%20%2F%2F%203d%20array%20declaration%0A%09%20int%5B%5D%5B%5D%20foo%20%3D%20new%20int%5B%5D%5B%5D%20%7B%0A%09%09%20%20%20%20%20%20%20%20new%20int%5B%5D%20%7B%201%2C%202%2C%203%20%7D%2C%0A%09%09%20%20%20%20%20new%20int%5B%5D%20%7B%201%2C%202%2C%203%2C%204%7D%2C%0A%09%09%09%20%20%20%20%7D%3B%0A%0A%09%09%09%20%20%20%20System.out.println(foo.length)%3B%20%20%20%20%2F%2F2%0A%09%09%09%20%20%20%20System.out.println(foo%5B0%5D.length)%3B%20%20%20%20%20%2F%2F3%0A%09%09%09%20%20%20%20System.out.println(foo%5B1%5D.length)%3B%20%20%20%20%20%2F%2F4%09%20%20%0A%09%7D%0A%7D” message=”” highlight=”” provider=”manual”/]
Array is Fixed in size.

  • If you trying to insert element more than size of Array then it will give Run time Exception.

[pastacode lang=”java” manual=”public%20class%20Array%20%0A%7B%0A%09public%20static%20void%20main(String%5B%5D%20args)%0A%09%7B%0A%20%20%20%20%20%09%09int%20b%5B%5D%3Dnew%20int%5B4%5D%3B%0A%09%09%20b%5B0%5D%3D12%3B%0A%09%09%20b%5B1%5D%3D13%3B%0A%09%09%20b%5B2%5D%3D14%3B%0A%09%09%20b%5B3%5D%3D12%3B%0A%09%09%20b%5B4%5D%3D12%3B%20%0A%09%09%20%2F%2F%20error%20array%20index%20out%20of%20bound%20because%20Array%20Size%20is%204%20%0A%09%09%20%2F%2F%20%20%20and%20we%20are%20trying%20to%20insert%205th%20element%0A%0A%09%09for(int%20i%3D0%3Bi%3C4%3Bi%2B%2B)%0A%09%09%7B%0A%09%09%09System.out.println(b%5Bi%5D)%3B%0A%09%09%7D%0A%20%20%20%20%20System.out.println(%22*******************************%22)%3B%0A%20%20%20%20%20%7D%0A%7D” message=”” highlight=”” provider=”manual”/]

Output:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 4
	at array.Array.main(Array.java:27)

Leave a Reply