Java long Array - long Array in Java, Initializing
long Array in Java
long Array
Java long Array
Java long array is used to store long data type values only in Java. The default value of the elements in a Java long array is 0. Java long array variable can also be declared like other variables with [] after the data type. The size of an array must be specified by an int value and not long or short. The long array index beginning from 0 in Java. Java array can also be used as a static field, a local variable or a method parameter. In this tutorial you can learn how to use long array in Java.
With the following Java long array examples you can learn
- What does long mean in Java ?
- What is long array in Java ?
- How to declare long Array in Java ?
- How to Initialize long Array in Java ?
- What is the Initial or Default Value of a long Array in Java ?
- What is the Length of an Array in Java ?
- How to assign values to Java long array ?
- How to get values from Java long array
- How to Convert long Array to String in Java ?
I found lot of people search with keywords like java long array, long array in java, long array, how to create long array, long array example from google, bing and other search engines. But in their search they like to find contents for big array in the sense they store huge amount of data or objects.
All the arrays are indexed and declared by int only. That is the size of an array must be specified by an int value and not long or short. All the arrays index beginning from 0 to ends at 2147483646. You can store elements upto 2147483647.If you try to store long (big) elements in array, you will get performance problems. If you overcome performance problems you should go to java collections framework or simply use Vector.
What does long mean in Java ?
long is a primitive data type in Java. long means numeric datatype. The minimum value of long is -9223372036854775808 and the maximum value of long is 9223372036854775807. It occupies 8 bytes memory. The default value of long is 0 (zero).
What is long array in Java ?
The array which holds primitive data type long in it.
How to declare long Array in Java ?
Arrays are declared with [] (square brackets). If you put [] (square brackets) after any variable of any type only that variable is of type array remaining variables in that declaration are not array variables those are normal variables of that type.
If you put [] (square brackets) after any data type all the variables in that declaration are array variables. All the elements in the array are accessed with index. The array element index is starting from 0 to n-1 number i.e. if the array has 5 elements then starting index is 0 and ending index is 4.
Following are the valid declaration statements for long array in Java.
long[] long_array_declaration1;
long long_array_declaration2[];
long []long_array_declaration3;
Related : Java boolean Array
Initializing long Array
How to Initialize long Array in Java ?
The long array will be initialized to 0 when you allocate it. All arrays in Java are initialized to the default value for the type. This means that arrays of ints are initialised to 0, arrays of longs are initialised to 0, arrays of booleans are initialised to false and arrays of reference types are initialised to null.
Following are the valid initialization statements for long array in Java.
long[] long_array_initialization1 = new long[10];
long long_array_initialization2[] = new long[10];
long []long_array_initialization3 = new long[10];
What is the Initial or Default Value of a long Array in Java ?
The initial value of a long array is 0 (zero). Because the default value of a long data type in Java is 0 (zero).
Can you index an array with a long int ?
An attempt to access an array component with a long index value results in a compile-time error. If for some reason you have an index stored in a long, just cast it to an int and then index your array. You cannot create an array large enough so it cannot be indexed by an integer in Java. If you cast long index to int index you will get java.lang.ArrayIndexOutOfBoundsException. ArrayIndexOutOfBoundsException is thrown to indicate that we are trying to access array element with an illegal index. This exception is thrown when the index is either negative or greater than or equal to the size of the array.
Related : Java byte Array
What is the Length of an Array in Java ?
In Java all the arrays are indexed and declared by int only. That is the size of an array must be specified by an int value and not long or short. All the arrays index beginning from 0 to ends at 2147483646. You can store elements upto 2147483647. If you try to store long (big) elements in array, you will get performance problems. If you overcome performance problems you should go to java collections framework or simply use Vector.
Syntax :
< data type > < variable >[];
< data type >[] < variable >;
Following example shows Java long Array . Save the following Java long Array Example program with file name JavaLongArrayExample.java .
Java long Array Example
public class JavaLongArrayExample { public static void main(String args[]) { // JAVA LONG ARRAY DECLARATION long a[]; // MEMORY ALLOCATION FOR JAVA LONG ARRAY a = new long[4]; // ASSIGNING ELEMENTS TO JAVA LONG ARRAY a[0] = 100000L; a[1] = 300000L; a[2] = 400000L; a[3] = 786777L; // LONG ARRAY OUTPUT System.out.println("Java long Array Example"); for(int i=0;i<a.length;i++) { System.out.println("Element at Index : "+ i + " " + a[i]); } } }
Related : Java char Array
Assigning Values to long Array
How to assign values to Java long array ?
Following Java long array example you can learn how to assign values to Java long array at the time of Java long array declaration.
Following example shows How to assign values to Java long array . Save the following assign values to Java long array Example program with file name AssignValuesToLongArray.java .
Assign values to Java long array at the time of Java long array declaration Example
public class AssignValuesToLongArray { public static void main(String args[]) { long a[] = {100000L,300000L,400000L,786777L}; System.out.println("Java long Array Example"); for(int i=0;i<a.length;i++) { System.out.println("Element at Index : "+ i + " " + a[i]); } } }
Related : Java short Array
How to declare Java long array with other Java long variables ?
Following Java long array example you can learn how to declare Java long array with other Java long variables.
Following example shows How to declare Java long array with other Java long variables . Save the following declare Java long array with other Java long variables Example program with file name DeclareJavaLongArrayWithLongVariables.java .
Declare Java long array with other Java long variables Example
public class DeclareJavaLongArrayWithLongVariables { public static void main(String args[]) { // a IS AN ARRAY a2 IS NOT AN ARRAY long a[], a2; a = new long[4]; a[0] = 100000L; a[1] = 300000L; a[2] = 400000L; a[3] = 786777L; a2 = 222222L; System.out.println("Java long Array Example"); System.out.println("a2 value is : "+a2); for(int i=0;i<a.length;i++) { System.out.println("Element at Index : "+ i + " " + a[i]); } } }
Related : Java int Array
How to assign Java long array to other Java long array ?
Following Java long array example you can learn how to assign Java long array to other Java long array.
Following example shows How to assign Java long array to other Java long array . Save the following assign Java long array to other Java long array Example program with file name AssignJavaLongArrayToLongArray.java .
Assign Java long array to other Java long array Example
public class AssignJavaLongArrayToLongArray { public static void main(String args[]) { long[] a, a2; a = new long[4]; a[0] = 100000L; a[1] = 300000L; a[2] = 400000L; a[3] = 786777L; a2 = a; System.out.println("Java long Array Example"); System.out.println("a array values"); for(int i=0;i<a.length;i++) { System.out.println("Element at Index : "+ i + " " + a[i]); } System.out.println("a2 array values"); for(int i=0;i<a2.length;i++) { System.out.println("Element at Index : "+ i + " " + a2[i]); } } }
Related : Java float Array
Converting long Array
How to Convert long Array to String in Java ?
The java.util.Arrays.toString(long[]) method returns a string representation of the contents of the specified long array. The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (a comma followed by a space).
The following example shows the usage of java.util.Arrays.toString() method.
Following example shows How to Convert long Array to String in Java . Save the following Convert long Array to String in Java Example program with file name ConvertLongArrayToString.java .
Convert Java long array to String in Java Example
import java.util.Arrays; public class ConvertLongArrayToString { public static void main(String args[]) { long long_array[] = {3, 7, 2, 1}; System.out.println("converted long array to String"); System.out.println(Arrays.toString(long_array)); } }
Related : Java double Array
How to Convert long Array to String in Java 8 ?
To convert long[] to String in Java 8, you can use the built-in Stream API for array operations in Java 1.8 and above. Following are the steps to convert long array to String in Java 8.
- First create LongStream by using Arrays.stream(long[])
- After use mapToObj() method, which will produce Stream<String>
- Convert long value to String by using String::valueOf as parameter for mapToObj() method.
- Use collect(Collectors.joining(",")) method to join each element with , ( comma ) as separator. You can use any separator as per your requirement.
Convert Java long array to String in Java 8 Example
Following example shows How to Convert long Array to String in Java 8 . Save the following Convert long Array to String in Java 8 Example program with file name ConvertLongArrayToStringJava8.java .
import java.util.Arrays; import java.util.stream.Collectors; public class ConvertLongArrayToStringJava8 { public static void main(String args[]) { long long_array[] = {3, 7, 2, 1}; String long_array_to_string = Arrays.stream(long_array).mapToObj(String::valueOf).collect(Collectors.joining(",")); System.out.println(long_array_to_string); } }
How to read data from Scanner to long array in Java ?
The Scanner class of the java.util package gives you methods like nextInt() , nextLong() , nextByte() , nextFloat() etc. to read data from keyboard . To read elements of long array from Scanner class use nextLong() method .
The following example shows how to read data from Scanner using nextInt() and nextLong() methods of Scanner class .
Following example shows How to read data from Scanner to long array in Java . Save the following read data from Scanner to long array in Java Example program with file name ReadDataFromScannerToLongArray.java . Please note in the ReadDataFromScannerToLongArray.java program the import statement import java.util.* is used in detail, the import statements java.util.Arrays, java.util.Scanner are used.
Read data from Scanner to long array in Java Example
import java.util.*; public class ReadDataFromScannerToLongArray { public static void main(String args[]) { // Scanner class declaration and instantiation Scanner s = new Scanner(System.in); System.out.println("Enter the length of long array"); int length = s.nextInt(); long long_array[] = new long[length]; System.out.println("Enter the elements of long array"); for(int i=0; i < length; i++) { long_array[i] = s.nextLong(); } System.out.println(Arrays.toString(long_array)); } }
How to Convert long Array to int Array in Java ?
To convert long array to int array, first you should create int[] array with you long[] array length. After that you iterate long array and cast each element of long array to int and put it into the int array.
Note : You are casting higher value to lower value, so you should check each element of long array for cast to int i.e. long array element value must be between -2147483648 and 2147483647. It is the min and max value of int data type in Java.
Following example shows How to Convert long Array to int Array in Java . Save the following Convert long Array to int Array in Java Example program with file name ConvertLongArrayToIntArray.java .
Convert long Array to int Array in Java Example
public class ConvertLongArrayToIntArray { public static void main(String args[]) { long[] long_array_values = {7, 8, 6, 1, 5, 3, 99}; int int_array_values[] = new int[long_array_values.length]; boolean successfully_converted = true; for(int i=0;i<long_array_values.length;i++) { if(long_array_values[i] >= Integer.MIN_VALUE && long_array_values[i] <= Integer.MAX_VALUE) { int_array_values[i] = (int) long_array_values[i]; } else { System.out.println("Element at " + i + " is not cast to integer value"); successfully_converted = false; } } if(successfully_converted) { System.out.println("long array to int array conversion successful"); } } }
How to Convert long Array to int Array in Java 8 ?
You can convert long array to int array in Java 8 by using java.util.Arrays.stream() method.
Following example shows How to Convert long Array to int Array in Java 8 . Save the following Convert long Array to int Array in Java 8 Example program with file name ConvertLongArrayToIntArrayJava8.java .
Convert long Array to int Array in Java 8 Example
import java.util.Arrays; public class ConvertLongArrayToIntArrayJava8 { public static void main(String args[]) { long[] long_array_values = {7, 8, 6, 1, 5, 3, 99}; int int_array_values[] = Arrays.stream(long_array_values).mapToInt(i -> (int) i).toArray(); System.out.println(Arrays.toString(int_array_values)); } }
How to Convert long Array to List in Java ?
To convert long array to List, first you create List with ArrayList. Then after you can add each element of long[] to List.
Following example shows How to Convert long Array to List in Java . Save the following Convert long Array to List in Java Example program with file name ConvertLongArrayToList.java . Please note in the ConvertLongArrayToList.java program the import statement import java.util.* is used in detail, the import statements java.util.ArrayList, java.util.List are used.
Convert long Array to List in Java Example
import java.util.*; public class ConvertLongArrayToList { public static void main(String args[]) { long[] long_array = {7, 8, 6, 1, 5, 3, 99}; List<Long> long_array_to_list = new ArrayList<>(); for(int i=0;i<long_array.length;i++) { long_array_to_list.add(long_array[i]); } System.out.println(long_array_to_list); } }
How to Convert long Array to List in Java 8 ?
You can convert long array to List in Java 8 by using java.util.Arrays.stream() method.
Following example shows How to Convert long Array to List in Java 8 . Save the following Convert long Array to List in Java 8 Example program with file name ConvertLongArrayToListJava8.java . Please note in the ConvertLongArrayToListJava8.java program the import statement import java.util.* is used in detail, the import statements java.util.Arrays, java.util.List are used.
Convert long Array to List in Java 8 Example
import java.util.*; import java.util.stream.Collectors; public class ConvertLongArrayToListJava8 { public static void main(String args[]) { long long_array[] = {3, 7, 2, 1}; List long_array_to_list = Arrays.stream(long_array).boxed().collect(Collectors.toList()); System.out.println(long_array_to_list); } }
How to Convert long Array to ArrayList in Java 8 ?
You can convert long array to ArrayList in Java 8 by using java.util.Arrays.stream() method.
Following example shows How to Convert long Array to ArrayList in Java 8 . Save the following Convert long Array to ArrayList in Java 8 Example program with file name LongArrayToArrayList.java . Please note in the LongArrayToArrayList.java program the import statement import java.util.* is used in detail, the import statements java.util.ArrayList, java.util.Arrays, java.util.List are used.
Convert long Array to ArrayList in Java 8 Example
import java.util.*; import java.util.stream.Collectors; public class LongArrayToArrayList { public static void main(String args[]) { long[] long_array = {7, 8, 6, 1, 5, 3, 99}; List long_array_to_list = Arrays.stream(long_array).boxed().collect(Collectors.toList()); ArrayList long_array_to_arraylist = new ArrayList(long_array_to_list); System.out.println(long_array_to_arraylist); } }
Sorting long Array
How to Sort long Array in Ascending Order using Java ?
To sort long[] in ascending order simply use java.util.Arrays.sort(long[] a); The java.util.Arrays.sort(long[] a) method sorts the specified array into ascending numerical order.
The sorting algorithm is a Dual-Pivot Quicksort by Vladimir Yaroslavskiy, Jon Bentley, and Joshua Bloch. This algorithm offers O(n log(n)) performance on many data sets that cause other quicksorts to degrade to quadratic performance, and is typically faster than traditional (one-pivot) Quicksort implementations.
Following example shows How to Sort long Array in Ascending Order using Java . Save the following Sort long Array in Ascending Order using Java Example program with file name LongArraySortAscendingOrder.java .
Sort long Array in Ascending Order using Java Example
import java.util.Arrays; public class LongArraySortAscendingOrder { public static void main(String args[]) { // INITIALIZING UNSORTED LONG ARRAY long long_array[] = {7, 8, 6, 1, 5, 3, 99}; System.out.println("Long Array Before Sorting"); System.out.println(Arrays.toString(long_array)); // SORTING LONG ARRAY IN ASCENDING ORDER Arrays.sort(long_array); System.out.println("Long Array After Sorting Ascending Order"); System.out.println(Arrays.toString(long_array)); } }
How to Sort long Array in Descending Order using Java ?
In Java 8 it is easy to sort long[] in descending order using Arrays.stream(). You have to use Comparator like Collections.reverseOrder() in Arrays.stream().boxed().sort() method.
Following example shows How to Sort long Array in Descending Order using Java . Save the following Sort long Array in Descending Order using Java Example program with file name LongArraySortDescendingOrder.java . Please note in the LongArraySortDescendingOrder.java program the import statement import java.util.* is used in detail, the import statements java.util.Arrays, java.util.Collections are used.
Sort long Array in Descending Order using Java Example
import java.util.*; public class LongArraySortDescendingOrder { public static void main(String args[]) { // INITIALIZING UNSORTED LONG ARRAY long long_array[] = {7, 8, 6, 1, 5, 3, 99}; System.out.println("Long Array Before Sorting"); System.out.println(Arrays.toString(long_array)); // SORTING LONG ARRAY IN DESCENDING ORDER long long_array_descending_order[] = Arrays.stream(long_array).boxed().sorted(Collections.reverseOrder()).mapToLong(i -> (long) i).toArray(); System.out.println("Long Array After Sorting Descending Order"); System.out.println(Arrays.toString(long_array_descending_order)); } }
Related : Java Arrays