What is the Long Array in Java with Example - onlyxcodes

Thursday, 11 May 2023

What is the Long Array in Java with Example

In this tutorial, you will learn long array in Java with examples. An array that contains items of the long data type, which is a 64-bit signed integer, is known as a long array in Java.


A long array in Java is no different from any other array in that it has a defined size and stores its elements in contiguous memory locations that can be accessed by their index.


The length property, which returns the array's element count, can be used to determine how long an array is.


long array in java

Table Content

1. How do you Declare a Long Array in Java?

2. How do you Initialize a Long Array in Java?

3. What is the Size of a Long Array in Java?

4. How to Create Empty Long Array in Java?

5. How to Print Long Array in Java?

6. How to Print Long Array using Arrays.toString() Method in Java?

7. How to Add Values to Long Aray in Java?

8. How to Iterate Long Array in Java?

9. How to Iterate Long Array using for each loop?

10. How to Convert long array into int array in Java?

11. How to Sort long Array in Java?

12. How to Sort the Long Array in Descending Order?

13. How to Return Long Array in Java?

14. Is there Long Array in Java?


How do you Declare a Long Array in Java?

In Java, we can use the following syntax to declare a long array:


arrayName indicates the name you want to give your array in this case, and arraySize indicates how many elements you want to store in the array.


long[] arrayName = new long[arraySize];

For instance, we might use the following code to create the long array with a capacity of 10 called myArray:


class LongArray
{
	public static void main(String args[])
	{
		long[] myArray = new long[10];
	}
}

How do you Initialize a Long Array in Java?

In Java, we can use either of the following approaches to initialize a long array:


Using the new keyword:


This method initializes the new long-type array object with the provided values.


import java.util.Arrays; 

class LongArray
{
	public static void main(String args[])
	{
		long[] arr = new long[]{1, 2, 3, 4, 5};
		
		System.out.println(Arrays.toString(arr));
	
	}
}

Output:


[1, 2, 3, 4, 5]

Using the Shorthand Syntax:


This approach is a shorthand for the first method, and it can only be used when the array is simultaneously declared and initialized.


Keep in mind that the number of values supplied within the curly brackets determines the size of the array in both situations.


import java.util.Arrays; 

class LongArray
{
	public static void main(String args[])
	{

		//Using the shorthand syntax:
		
		long[] arr = {1, 2, 3, 4, 5};
		
		System.out.println(Arrays.toString(arr));
	}
}

Output:


[1, 2, 3, 4, 5]

What is the Size of a Long Array in Java?

In Java, we can use the following syntax to build long arrays:


The number of elements in the array is indicated in this code by the integer value numElements. The size of the long array is determined by multiplying the number of elements by the Java long data type size, which is 8 bytes.


long[] myArray = new long[numElements];

As an illustration, if we make a long array with 10 elements, it will be 80 bytes in size (10 elements x 8 bytes for each element). 


Remember that this is only the array's size; if the program uses any additional memory (such as for variables, objects, etc.), the program's overall memory use will go up.


long[] myArray = new long[10];

How to Create Empty Long Array in Java?

The following Java code can be used to construct an empty long array:


This produces a lengthy array with the name myArray and a length of 0. 


Using methods like Arrays.copyOf() or manually assigning values to certain indices, you can later add elements to the array.


class LongArray
{
	public static void main(String args[])
	{
		long[] myArray = new long[0];
	}
}

How to Print Long Array in Java?

Java programmers can use a loop to run through the elements of a long array and then output each one using the System.out.println() method. 


Here's an illustration:


In this example, we've added 10 elements to a long array called arr. Then, each element of the array is iterated over using a for loop, and it is printed using the System.out.println() method.


class LongArray
{
	public static void main(String args[])
	{
		long[] arr = {1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L};
		
		for (int i = 0; i < arr.length; i++) 
		{
			System.out.println(arr[i]);
		}

	}
}

Output:


1
2
3
4
5
6
7
8
9
10

How to Print Long Array using Arrays.toString() Method in Java?

As an alternative, we can output the full array on a single line by using the Arrays.toString() method. 


Here's an illustration:


This will print the full array, enclosed in square brackets and separated by commas, on a single line.


import java.util.ArrayList;
import java.util.Arrays; 

class LongArray
{
	public static void main(String args[])
	{
		long[] arr = {1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L};
		
		System.out.println(Arrays.toString(arr));
	}
}

Output:


[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

How to Add Values to Long Aray in Java?

In Java, we may use a loop to repeatedly iterate over the array and add values at each index. 


Here's an illustration:


In this example, we'll create a brand-new long array with a length of 5, then iterate over the array using a for loop. 


We use the index variable i to assign a value to the current index throughout each iteration. To prevent giving any array element a value of 0, we add 1 to i.


long[] myArray = new long[5];

for (int i = 0; i < myArray.length; i++) 
{
	myArray[i] = i + 1;
}

Select the value to be inserted, along with its index:


int index = 2;
int value = 10;

Create a new long array that is greater than the old one in length:


long[] newArray = new long[myArray.length + 1];

Copies the items of the original array into the new array up to the index where we want to place the value:


for (int i = 0; i < index; i++) 
{
	newArray[i] = myArray[i];
}

Put the new value in the desired location:


newArray[index] = value;

Copy the final few elements from the original array and then add them to the new array:


for (int i = index; i < myArray.length; i++) 
{
	newArray[i + 1] = myArray[i];
}

To finish, assign the new array to the initial array variable.


myArray = newArray;

At the provided index, the new value will now be added to the myArray array.


Full Code,


import java.util.ArrayList;
import java.util.Arrays; 

class LongArray
{
	public static void main(String args[])
	{
		long[] myArray = new long[5];
		
		for (int i = 0; i < myArray.length; i++) 
		{
			myArray[i] = i + 1;
		}
		
		System.out.println("Orignal array value " +Arrays.toString(myArray));
		
		int index = 2;
		int value = 10;
		
		System.out.println("-----------------------------");
		
		long[] newArray = new long[myArray.length + 1];
		
		for (int i = 0; i < index; i++) 
		{
			newArray[i] = myArray[i];
		}
		
		newArray[index] = value;
		
		for (int i = index; i < myArray.length; i++) 
		{
			newArray[i + 1] = myArray[i];
		}

		myArray = newArray;
		
		System.out.println("After Add value " +Arrays.toString(myArray));
		
	}
}

Output:


We can see from the result that we added a new array value of 10 to position 2.


Orignal array value [1, 2, 3, 4, 5]
-----------------------------
After Add value [1, 2, 10, 3, 4, 5]

How to Iterate Long Array in Java?

Java allows you to use either a for loop or a for-each loop to iterate through a long array. 


Here is an illustration of how to use a for loop:


This example uses the loop variable i to run through the items of the myArray array. 


The loop continues as long as i is less than myArray.length, the length of the array. 


I used myArray[i] inside the loop to access the current array element, which is then displayed to the console using System.out.println().


import java.util.ArrayList;
import java.util.Arrays; 

class LongArray
{
	public static void main(String args[])
	{
		long[] myArray = {1L, 2L, 3L, 4L, 5L};
		
		for (int i = 0; i < myArray.length; i++) 
		{
			System.out.println(myArray[i]);
		}		
	}
}

Output:


1
2
3
4
5

How to Iterate Long Array using for each loop?

As an alternative, we can traverse through the array using a for-each loop:


The for-each loop in this illustration assigns each element of the myArray array to the loop variable element as it iterates over the array's components. 


Within the loop, element is used to get the current element, and System.out.println() is used to print that element to the console. 


Although the for-each loop is shorter and easier to read than the for loop, it does not give access to the index of the current element.


import java.util.ArrayList;
import java.util.Arrays; 

class LongArray
{
	public static void main(String args[])
	{
		long[] myArray = {1L, 2L, 3L, 4L, 5L};
		
		for (long element : myArray) 
		{
			System.out.println(element);
		}		
	}
}

Output:


1
2
3
4
5

How to Convert long array into int array in Java?

Java allows you to use a loop to iterate through each element of a long array and apply type casting to change each element into an integer. 


Here is an illustration of some code:


In this example, I first define a long array with three members, longArray. 


Then, with the same length as longArray, I create an integer array called intArray. 


Each element of longArray is iterated over using a for loop, and then each element is converted to an int using the (int) syntax. The resulting integer value is then assigned to the matching intArray element.


It should be noted that the cast will produce an invalid value if any element in longArray exceeds Integer.MAX_VALUE or falls below Integer.MIN_VALUE. You might need to handle the conversion differently or use a different data type in such circumstances.


import java.util.ArrayList;
import java.util.Arrays; 

class LongArray
{
	public static void main(String args[])
	{
		long[] longArray = {10000000L, 20000000L, 3000000L};
		
		int[] intArray = new int[longArray.length];

		
		for (int i = 0; i < longArray.length; i++) 
		{
			intArray[i] = (int) longArray[i];
		}

		System.out.println(Arrays.toString(intArray));
	}
}

Output:


[10000000, 20000000, 3000000]

How to Sort long Array in Java?

The Arrays.sort() method found in the java.util package can be used to sort a long array in Java. The following example of code shows how to sort a long array in ascending order:


We have a longArray, an array of longs, in this example. 


The array is passed as a parameter when we use the Arrays.sort() method. This uses ascending order to sort the array.


Finally, I use the Arrays.toString() method to print the sorted array.


import java.util.ArrayList;
import java.util.Arrays; 

class LongArray
{
	public static void main(String args[])
	{
		long[] longArray = {5L, 2L, 8L, 1L, 9L};
		
		System.out.println("orignal array " +Arrays.toString(longArray));
		
		Arrays.sort(longArray);
		
		System.out.println("after sorting " +Arrays.toString(longArray));
	}
}

Output:


orignal array [5, 2, 8, 1, 9]
after sorting [1, 2, 5, 8, 9]

How to Sort the Long Array in Descending Order?

Use the Arrays.sort() method along with a Comparator that sorts in reverse order to sort the array in descending order. 


In this example, I use the Comparator.reverseOrder() method to sort an array of elements in reverse order before calling the Arrays.sort() method. This uses descending order to sort the array.


Here's an illustration:


import java.util.Arrays; 
import java.util.Comparator;

class LongArray
{
	public static void main(String args[])
	{
		Long[] longArray = {5L, 2L, 8L, 1L, 9L};
		
		System.out.println("orignal array " +Arrays.toString(longArray));
		
		Arrays.sort(longArray, Comparator.reverseOrder());
		
		System.out.println("after sorting in reverse order " +Arrays.toString(longArray));
	}
}

Output:


orignal array [5, 2, 8, 1, 9]
after sorting in descending order [9, 8, 5, 2, 1]

How to Return long Array in Java?

These steps can be used in Java to return a large array:


Declare the method and a long array as the return type. 


For instance:


public long[] getLongArray() 
{
	// method implementation
}

With the specified size, create a new long array and fill it with values.


public long[] getLongArray() 
{   
	long[] arr = new long[5];
	   
	arr[0] = 1L;
	   
	arr[1] = 2L;
	   
	arr[2] = 3L;  // populate array
	   
	arr[3] = 4L;
	   
	arr[4] = 5L; 
}

Using the return keyword, the method should return the long array. 


public long[] getLongArray() 
{   
	long[] arr = new long[5];
	   
	arr[0] = 1L;
	   
	arr[1] = 2L;
	   
	arr[2] = 3L;  // populate array
	   
	arr[3] = 4L;
	   
	arr[4] = 5L;

	return arr;	 //return array
}

Once we have the long array, you can use it in your program by calling this function. 


long[] myArray = getLongArray();

Full Code,


class LongArray
{
	public static void main(String args[])
	{
		long[] myArray = getLongArray();
		
		System.out.println(Arrays.toString(myArray));
	}
	
	public static long[] getLongArray() 
	{
		long[] arr = new long[5];
			
		arr[0] = 1L;
		arr[1] = 2L;
		arr[2] = 3L;
		arr[3] = 4L;
		arr[4] = 5L;
				
		return arr;
	}
}

Output:


 [1, 2, 3, 4, 5]

Is there Long Array in Java?

In Java, long arrays that can store a series of long integer values are supported.


In Java, we can use the following syntax to declare a long array:


The length in this case indicates how many elements are in the array. Using the array's index, which starts at 0, you can then access and modify individual elements.


 long[] arr = new long[length];

As an illustration, we could put the following values into the array:


arr[0] = 100;
arr[1] = 200;
arr[2] = 300;

The following is how you can get values out of the array:


import java.util.Arrays; 

class LongArray
{
	public static void main(String args[])
	{
		long[] arr = new long[3];
		
		arr[0] = 100;
		arr[1] = 200;
		arr[2] = 300;

		System.out.println(Arrays.toString(arr));
	}
}

[100, 200, 300]

Learn More:

Can you print a string array in Java

Can you Remove an Element from an Array in Java

Can we compare 2 arrays in Java?

How to Create a 2D Array Dynamically in Java

What is Array in Java with Example

What is Difference Between Array and ArrayList in Java

How to Add Value to an Array in Java

No comments:

Post a Comment

Post Bottom Ad