Previous
Next

Java long Array - long Array in Java, Initializing

Last updated on

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

  1. What does long mean in Java ?
  2. What is long array in Java ?
  3. How to declare long Array in Java ?
  4. How to Initialize long Array in Java ?
  5. What is the Initial or Default Value of a long Array in Java ?
  6. What is the Length of an Array in Java ?
  7. How to assign values to Java long array ?
  8. How to get values from Java long array
  9. 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.

Share This Page On :

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;

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.

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 >;

Java long Array Example

/*  Java long Array Example
    Save with file name LongArray.java  */
   
public class LongArray
{
  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]);
    }
  }
}

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.

How to assign values to Java long array at the time of Java long array declaration

/*  How to assign values to Java long array at the time of Java long array declaration Example
    Save with file name LongArray2.java */
   
public class LongArray2
{
  public static void main(String args[])
  {
    //JAVA LONG ARRAY DECLARATION
    long a[] = {100000L,300000L,400000L,786777L};
    //JAVA 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]);
    }
  }
}

Following Java long array example you can learn how to declare Java long array with other Java long array variables.

How to declare Java long array with other Java long array variables

/*  How to declare Java long array with other Java long array variables Example
    Save with file name LongArray3.java */
   
public class LongArray3
{
  public static void main(String args[])
  {
    //JAVA LONG ARRAY DECLARATION
    //a IS AN ARRAY a2 IS NOT AN ARRAY
    long a[], a2;
    //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;
    a2 = 222222L;
    //JAVA LONG ARRAY OUTPUT
    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]);
    }
  }
}

Following Java long array example you can learn how to assign Java long array to other Java long array.

How to assign Java long array to other Java long array

/*  How to assign Java long array to other Java long array Example
    Save with file name LongArray4.java */
   
public class LongArray4
{
  public static void main(String args[])
  {
    //JAVA LONG ARRAY DECLARATION
    long[] a, a2; //a AND a2 ARE ARRAY VARIABLES
    //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;
    //ASSIGNING a ARRAY TO a2 ARRAY VARIABLE
    a2 = a;
    //JAVA LONG ARRAY OUTPUT
    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]);
    }
  }
}

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.

How to Convert Java long array to String in Java Example

/*  How to Convert Java long array to String Example
    Save with file name ConvertLongArrayToString.java */
import java.util.Arrays;
public class ConvertLongArrayToString
{
  public static void main(String args[])
  {
    //LONG ARRAY DECLARATION
    long long_array[] = {3, 7, 2, 1};
    System.out.println("converted long array to String");
    System.out.println(Arrays.toString(long_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.

  1. First create LongStream by using Arrays.stream(long[])
  2. After use mapToObj() method, which will produce Stream<String>
  3. Convert long value to String by using String::valueOf as parameter for mapToObj() method.
  4. Use collect(Collectors.joining(",")) method to join each element with , ( comma ) as separator. You can use any separator as per your requirement.

How to Convert Java long array to String in Java 8 Example

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 .

How to read data from Scanner to long array Example

How to read data from Scanner to long array Example

/*  How to read data from Scanner Example
    Save with file name ScannerToLongArray.java */
  
import java.util.Arrays;
import java.util.Scanner;
  
public class ScannerToLongArray
{
  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.

How to 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.

How to 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.

How to Convert long Array to List in Java Example

import java.util.ArrayList;
import java.util.List;
 
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.

How to Convert long Array to List in Java 8 Example

import java.util.Arrays;
import java.util.List;
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.

How to Convert long Array to ArrayList in Java 8 Example

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
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);
  }
}

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.

How to 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.

How to Sort long Array in Descending Order using Java Example

import java.util.Arrays;
import java.util.Collections;
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));
  }
}
    

More Related Tutorials

Previous
Next