Why use array?
To store the marks of all the students of a university,you need to declare thousands of variables.
In addition each variable name needs to be unique,as per the variable naming rules.
It is practically impossible for a person to remember the name of each variable.
To avoid such situations,we can use array.
What is array?
Array is a collection of homogeneous data elements. It stores the value on the basis of the index value.
It is fixed in size means that you can't increase the size of array at run time.
Index of array starts from zero to size-1.
Every array in a Java is an object, Hence we can create array by using new keyword.
By using loop we can access all the elements of array.
Types of Array
Single Dimensional Array
Multidimensional Array
How to declare 1D array?
Syntax
Datatype arrayname[]; int numbers[];
How to allocate memory to a 1D array:
Syntax
datatype arrayname=new datatype[size]; int numbers[]=new int[10];
How to initialize 1D array?
Syntax
Datatype arrayname[]={value1,value2…..valuen}; int numbers[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
How to print values of 1D array?
Print values of array
public class Demo{ public static void main(String args[]){ int a[] = {10,20,30,40,50}; for (int i=0; i<5; i++){ System.out.println(a[i]); } } }Try it Yourself
Exercise Q&A
arr = new int[5];
public class array_output { public static void main(String args[]) { int array_variable [] = new int[10]; for (int i = 0; i < 10; ++i) { array_variable[i] = i; System.out.print(array_variable[i] + " "); } } }
How to declare 2D array?
Syntax
Datatype arrayname[][]; int numbers[][];
How to allocate memory to a 2D array:
Syntax
Arrayname[][]=new datatype[row][column]; int numbers [][]=new int[5][5];
How to initialize 2D array?
Syntax
Datatype arrayname[][]={ {row1…..row1n}, {row2….row2n}, }; int m[][] = {{ 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 }};
How to print values of 2D array?
Print values of 2D array
public class Demo { public static void main(String args[]) { int i, j; int m[][] = {{ 1, 2, 3, 4 }, { 5, 6, 7, 8 }, { 9, 10, 11, 12 }, { 13, 14, 15, 16 }}; for(i=0; i<4; i++){ for(j=0; j<4; j++){ System.out.print(m[i][j] + " "); } System.out.println(); } } }Try it Yourself
Solve this!!
double[][] values = { {1.2, 9.0, 3.2}, {9.2, 0.5, 1.5, -1.2}, {7.3, 7.9, 4.8} } ;
public class array_output { public static void main(String args[]) { int array_variable[][] = {{ 1, 2, 3}, { 4 , 5, 6}, { 7, 8, 9}}; int sum = 0; for (int i = 0; i < 3; ++i) for (int j = 0; j < 3 ; ++j) sum = sum + array_variable[i][j]; System.out.print(sum / 5); } }
- Collection
- ArrayList
- LinkedList
- Vector
- HashSet
- Conditional Statements
- Classes and Objects
- Inheritance
- Packages
- Exception Handling
- Threads
- TreeSet
- LinkedHashSet
- HashMap
- TreeMap
- LinkedHashMap
- HashTable
- Iterator and ListIterator
- String
- Scanner
- Array Small Programs
- Introduction
- Variables
- DataTypes
- Operators
- Conditional Statements
- Loop
- Arrays
- Object Oriented Programming
- Classes and Objects
- Access Modifiers
- Methods
- Abstraction
- Constructor
- Packages
- StringBuffer
- Inheritance
- StringBuilder
- Encapsulation
- Java IO
- Polymorphism
- Interface
- Types Of Variables
- Static Keyword
- Final Keyword
- Super Keyword
- This Keyword
- Exception Handling