Using a long as ArrayList index in java

后端 未结 7 1530
遇见更好的自我
遇见更好的自我 2020-12-10 14:01

I am writing this java program to find all the prime numbers up to num using the Sieve of Eratosthenes, but when I try to compile, it says I can\'t use a long var as an arra

7条回答
  •  余生分开走
    2020-12-10 14:54

    I'm not sure why your code would compile to begin with.

    You're not supposed to use [] in an array list to access members. An arraylist is merely a list that is internally stored in an array. You have to use the list get operation (which would still be O(1)). Writing numlist[index] means that you have an array of objects in numlist. You cannot override the [] operation as in C++.

    In addition, an int is 32 bits in Java. Having an array of length greater than 2^32 (so you would need long indices) is unlikely and I'm not even sure the specification allows it.

提交回复
热议问题