java – how to display all the elements of a long array?

Question:

I have an array long m[] from 1 to 100000000000. How can I display it? (and how to work with it exactly in the global plan "find out the size", etc.) I wrote such a code, but it only works for numbers up to a maximum of int-a.

public static void main(String[] args) {
    // TODO Auto-generated method stub
    long m[] =  {1, 2, 3, 4, 5, 8, 9, 10};

    try {sorter(m);} 
    catch(Exception e) {System.out.println("ошибка");}
}

static void sorter(long m[]) {
    //.length это интовая встрoенная функция ((((
    for (long i=0; i<m.length;i++) {
        System.out.println("число "+ m[(int) i]);
    }
}

Answer:

If you need to store the number of elements greater than Integer.MAX_VALUE , then I advise you to look at LinkedList . To find out the size, it is not enough just to call the size() method, because it is limited to the same Integer.MAX_VALUE . As an option, create a counter, and go through all the elements in the iterator, incrementing this counter:

Iterator<Long> iterator = linkedList.iterator();
long count = 0;
while(iterator.hasNext()) {
   iterator.next();
   count++;
}
System.out.println(count);

Or using the default forEachRemaining method (for java8) :

AtomicLong count = new AtomicLong(0);
Iterator<Long> iterator = linkedList.iterator();
iterator.forEachRemaining(next -> count.incrementAndGet());
System.out.println(count.get());
Scroll to Top