Arrays in Java
Arrays are the type of data structure that can store data of similar data types. Arrays are allocated in contiguous memory allocation with a fixed size.
Syntax:
// Both Methods are correct
int arr[]=new int[20];
int[] arr=new int[20];
Below is the implementation of the above method:
Java
// Java Program to implement // arrays class GFG { public static void main(String[] args) { // declares an Array of integers. int [] arr = new int [ 5 ]; // Allocating memory to the // elements arr[ 0 ] = 10 ; arr[ 1 ] = 20 ; arr[ 2 ] = 30 ; arr[ 3 ] = 40 ; arr[ 4 ] = 50 ; // accessing the elements of the specified array for ( int i = 0 ; i < arr.length; i++) System.out.println( "arr[" + i + "] :" + arr[i]); } } |
arr[0] :10 arr[1] :20 arr[2] :30 arr[3] :40 arr[4] :50
Multidimensional Arrays in Java
Arrays are not bounded to be single-dimensional but can store elements in multidimensional.
Syntax:
int[][] arr= new int[3][3];
int arr[][]=new int[3][3];
Below is the implementation of the above method:
Java
// Java Program to implement // 2-D dimensional array // driver class public class multiDimensional { // main function public static void main(String args[]) { // declaring and initializing 2D array int arr[][] = { { 1 , 2 , 3 }, { 4 , 5 , 6 }, { 7 , 8 , 9 } }; // printing 2D array for ( int i = 0 ; i < 3 ; i++) { for ( int j = 0 ; j < 3 ; j++) System.out.print(arr[i][j] + " " ); System.out.println(); } } } |
1 2 3 4 5 6 7 8 9
Java Cheat Sheet
Java is a programming language and platform that has been widely used since its development by James Gosling in 1982. It follows the Object-oriented Programming concept and can run programs written in any programming language. Java is a high-level, object-oriented, secure, robust, platform-independent, multithreaded, and portable programming language All those words are collectively called Java Buzzwords. It is commonly used for programming web-based, window, enterprise, and mobile applications. This Java Cheat Sheet article has been written by experts in Java and based on the experience of students who have recently undergone Java interviews.
This Core Java Cheat Sheet has been designed by Java experts, based on the experience of students who have recently undergone Java interviews. Whether you are a beginner or an experienced Java developer, this Java Cheat Sheet is a valuable resource for quickly accessing essential syntax, concepts, and best practices related to Java Programming.