23

I'm trying to create an array of long in java, but eclipse is showing me an error as shown below:

Below is my code:

enter image description here

How can I resolve this?

Can't i create a long size array in java?

Community
  • 1
  • 1

8 Answers8

27

Arrays of longs are fine: long[]. But all arrays are int-indexed. So just change long n to int n in the method parameter declaration, and you'll be all set.

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
2

i need an array of 10^9 elements

You can create an array of one billion using an int value. Make n an int, and you can create an array with new long[n] Note: this will use 8 GB of heap.

Since you are building all the elements using a formula, you should be able to give all the values dynamically. i.e. create a component which is a "virtual" array which would save you having to generate an array of them all.

The reason I point this out is that the second loop is likely to take minutes at best if k is very small, or could easily take days.

BTW: I can't see all the code, but it appears you never need more than k+1 values which can be allocated once instead of n-k times.

Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
1

For index you have to use int but not long

Festus Tamakloe
  • 11,231
  • 9
  • 53
  • 65
0

n (the array capacity) has to be an integer not long

Jiri Kremser
  • 12,471
  • 7
  • 45
  • 72
0

You have my sympathy. We go through this every time memory sizes increase. There is a strange expectation that this time array sizes will not need to increase in parallel with memory sizes.

Your best solution is probably to write your own class with long get(long index) and void set(long value, long index) methods. It could represent the data as a long[10][1000000000], and encapsulate the mapping between the natural long index and the actual pair of int indices.

Patricia Shanahan
  • 25,849
  • 4
  • 38
  • 75
0

Please note that array size is always equal to int size. if you specify the array size more than 2147483647 you will get error. long n; long a[]=new long[n]; this will create error because long n exceeds 2147483647. if int n then no error occurs.

0
import java.util.*;
public class Main
{
public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    long[] arr = new long[n];//Just declare the size of array of int type , because we cannot declare the size of array as long type ,rather we can store the long type in it ,This code will fix the error .

    for(int i=0;i<n;i++)
    {
        arr[i]=sc.nextLong();
    }
    for(long i:arr)
     {
        System.out.print(i+" ");
     }
    }
}
  • 1
    Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Rathan Naik Jul 06 '22 at 17:39
0

The index needs to be int so I convert long to int.

long n = sc.nextLong();
long[] arr = new long[(int)n];
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
Abubakar Khalid
  • 109
  • 2
  • 5
  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/32931107) – Attila T Oct 17 '22 at 13:19
  • This is the right answer. I have checked and then answer the question. – Abubakar Khalid Oct 18 '22 at 01:05
  • Sorry, I got your point. My Answer can solve the problem but this is the wrong way. because he can use int instead of converting long to int. – Abubakar Khalid Oct 18 '22 at 01:41