Ads by ProfitSence
Close

How to Create an Array in Java

Last Updated on Wednesday 5th Oct 2022

Create Array Java

Java Array You can either use

  • Array declaration
  • Array literal ( cannot be used for re-assigning an array ).
			
					int[] myIntArray = new int[3];
int[] myIntArray = {1, 2, 3};
int[] myIntArray = new int[]{1, 2, 3};

			
	

How to Declare an Array in Java

Multidimensional array

Declaration

			
					int[][] num = new int[5][2];

			
	

Initialization

			
					 num[0][0]=1;
 num[0][1]=2;
 num[1][0]=1;
 num[1][1]=2;

			
	

int array java

			
					int[][] num={ {1,2}, {1,2}, {1,2}, {1,2}, {1,2} };

			
	

declare int array java

			
					float floatArray[]; // Initialize later
int[] integerArray = new int[10];
String[] array = new String[] {"a", "b"};

			
	

For Accessing

			
					for (int[] a : num) {
  for (int i : a) {
    System.out.println(i);
  }
}

			
	

Example

You have six integer elements which are 1, 2, 3, 4, 5 and 6 in an array. You can do it in the following way.

Basic pattern is for initialization and declaration.

			
					datatype[] arrayname = new datatype[requiredarraysize];

			
	
			
					int[] a = new int[6];

			
	

or

			
					int[] a = {1, 2, 3, 4, 5, 6};

			
	

Declare string array java

			
					// string array
String[] a = {"ab", "cde", "fghi"};

// character array
char[] a = {'a', 'b', 'c'};

// For float double
double[] a = {1.2, 1.3, 12.3};

			
	
how to create an array in java
create array java
java create array
how to make an array in java
create array in java
array literal java
how to create a new array in java
create an array java