One dimensional array in Java

In this article, we will discuss all the details of One-dimensional array in java or single dimensional array java.
Here is the table content of the article will we will cover this topic.
1. Declaration of One-dimensional array in java?
2. Construction of One-dimensional array in java?
3. Memory Representation after Construction?
4. Initialization of One-dimensional array in java?
5. Memory Representation after Initialization?
6. Example of an Array

One Dimensional Array in java is always used with only one subscript([]). A one-dimensional array behaves likes a list of variables. You can access the variables of an array by using an index in square brackets preceded by the name of that array. Index value should be an integer.

Steps:

  • Declaration  of Array
  • Construction of Array
  • Initialization of Array

Declaration of One-dimensional array in java

Before using the array, we must declare it. Like normal variables, we must provide data type of array and name of an array. Data type means which type of elements we want to store in Array. So, we must specify the data type of array according to our needs. We also need to specify the name of an array so that we can use it later by name.

datatype[] arrayName;
Or 
datatype arrayName[];
Or 
datatype []arrayName;
  • datatype can be a primitive data type(int, char, Double, byte etc.)or Non-primitive data (Objects).
  • arrayName is the name of an array
  • [] is called subscript.
 int[] number;
 Or 
 int number[];
 or
 int []number;  

Construction of One-dimensional array in java

For the creation of an array new keyword is used with a data type of array. You must specify the size of the array. The size should be an integer value or a variable that contains an integer value. How many elements you can store an array directly depends upon the size of an array.

arrayName = new DataType[size]; 

new Datatype[size]: It creates an array in heap memory. Because an array is an object, so it stored in heap memory.

arrayName: Assignment operator assigns the newly created array to the reference of variable arrayName. We can access the elements of an array by use of array names.

number =  new int[10];  // allocating memory to array

In this example new int[10] creates a new array of int type in heap memory. The assignment operator assigns the array object to the reference variable number. Now you can access the array by use of the number. You can access each value of array by using numbers with subscript([]). We must have to use only one subscript([]) for One dimensional array in java We will discuss it later in the Access of array.

One dimensional array in java

Note: When we are creating a new array the elements in the array will automatically be initialized by their default values. For e.g. : zero (int types), false (boolean), or null (for object types).

Memory Representation after Construction

One dimensional array in java

The index of an array starts from 0 and ends with length-1. The first element of an array is number[0], second is number[1] and so on. If the length of an array is n, the last element will be arrayName[length-1]. Since the length of the number array is 10, the last element of the array is number[9]. 

Initialization of One-dimensional array in java

To initialize the Array we have to put the values at each index of array. In above section we have created a Array with size 10. So now we will see how we can add values in Array.

public class MyExample 
{
	public static void main(String[] args) 
	{
		// Declaration of Array
		int[] number;
		
		// Construction of Array with given size 
		// Here we are giving size 10 it mean it can hold 10 values of int type
		number =  new int[10];
		
		// Initialization of Array
		number[0] = 11;
		number[1] = 22;
		number[2] = 33;
		number[3] = 44;
		number[4] = 55;
		number[5] = 66;
		number[6] = 77;
		number[7] = 88;
		number[8] = 99;
		number[9] = 100;
		
		//Print the values from Array
		for(int i = 0; i < number.length; i++)
			System.out.println(number[i]);
		
     }
}

Output: 11
22
33
44
55
66
77
88
99
100

You can also construct an array by initializing the array at declaration time. If you are initializing the array it is another way to construct an array.

dataType arrayName[] = {value1, value2, …valueN} 

Some point about Initialization:

  • If you choose datatype of an array is int then values should be int type. It means a type of value is totally dependent on datatype of an array.
  • Value1 will be store at 0 indexes, value2 in 1 index and so on.
  • The size of the array depends upon how many values you are providing at the time of initialization.
int  number[] = {11, 22, 33 44, 55, 66, 77, 88, 99, 100} 

It’s created an array of int type as you see we are initializing it during declaration.

Memory Representation after Initialization

One dimensional array in java

Example of an Array

Here we will see different examples of Java Array and How to use array in java. We will also see how to initialize array with values java (java initialize array with values).

So Let’s pick the first example, Here we will show how we can create the array and initialize the values at the same time. Here we will do two operations in single-step i.e. array declaration and initialization in java.

public class MyExample 
{
	public static void main(String[] args) 
	{
		// Creating a int Array with with values
		int[] number = new int[] { 11, 22, 33, 44, 55, 66, 77, 88, 99};
		System.out.println("Print int Array:");
		for(int i = 0; i < number.length; i++)
		{
			System.out.println("Values on index "+ i + ": " + number[i]);
		}
		
		// Creating a String Array with with values
		String[] names = new String[] { "Java", "Goal", "Learning", "Website", "for", "Java", "Concepts"};
		System.out.println("Print String Array:");
		for(int i = 0; i < names.length; i++)
		{
			System.out.println("Values on index "+ i + ": " + names[i]);
		}
	}
}

Output: Print int Array:
int Array values on index 0: 11
int Array values on index 1: 22
int Array values on index 2: 33
int Array values on index 3: 44
int Array values on index 4: 55
int Array values on index 5: 66
int Array values on index 6: 77
int Array values on index 7: 88
int Array values on index 8: 99
Print String Array:
String Array values on index 0: Java
String Array values on index 1: Goal
String Array values on index 2: Learning
String Array values on index 3: Website
String Array values on index 4: for
String Array values on index 5: Java
String Array values on index 6: Concepts

Here we are creating an array and initializing the values. The size of the array depends on the number of elements. The JVM allocates the size to the array is equal to the number of elements presented in braces. The first element stores at index 0 and the last element stores at length – 1.

1. Quiz, Read the below code and do answer.

Which of the following is used for declaration, construction, and initialization of an array at the same time?

Click on anyone to know the answer.

2. Quiz, Read the below code and do answer.

Which of the following is the advantage of the java array?

Click on anyone to know the answer.

3. Quiz, Read the below code and do answer.

Select the correct option

Click on anyone to know the answer.

4. Quiz, Read the below code and do answer.

public class MyExample
{
        public static void main(String[] args)
        {
            int[] a = new int[0];
            System.out.println(a.length);
        }
}

 

Click on anyone to know the answer.

5. Quiz, Read the below code and do answer.

public class MyExample
{
        public static void main(String[] args)
        {
           int[] a = new int[3];
      	   System.out.println("a[0] :" + a[0]);
        }
}

Click on anyone to know the answer.

6. Quiz, Read the below code and do answer.

public class MyExample
{
    public static void main(String[] args)
    {
        double[] myArray = {10 ,55, 55, 55, 55, 10};
     	double max = myArray[0];
     	int indexOfMax = 0;
     	for(int i = 1; i < myArray.length ; i++)
     	{
     		if(myArray[i]>max)
     		{
     			max = myArray[i];
     			indexOfMax=i;
     		}
     	}
       	 System.out.println(indexOfMax);
      }
}

Click on anyone to know the answer.

7. Quiz, Read the below code and do answer.

public class MyExample
{
    public static void main(String[] args)
    {
        int[] array1 = new int[5];
        array1 = new int[6];
    }
}

Click on anyone to know the answer.

8. Quiz, Read the below code and do answer.

public class MyExample
{
    public static void main(String[] args)
    {
        int[] myArray = new int[4];
        myArray[1] = 1;
        myArray = new int[2];
        System.out.println("myArray[1] : "+ myArray[1]);
    }
}

Click on anyone to know the answer.

5 thoughts on “One dimensional array in Java”

Leave a Comment