http://stackoverflow.com/questions/459643/using-a-long-as-arraylist-index-in-java

http://bbs.csdn.net/topics/390207278

——————————————————————————————————————————————————————————

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 array index, and it expects an int var in its place. But I'll be working with large numbers, so I can't use int. What can I do?

import java.util.*;
import java.lang.*; public class t3{
public static void main(String[] args){
long num = 100; //declaring list and filling it with numbers
ArrayList<Long> numlist = new ArrayList<Long>();
for(long x=2 ; x<num ; x++){
numlist.add(new Long(x));
} //sieve or eratosthenes
for(long x=0 ; x<Math.sqrt(num) ; x++){
for(long y=x+1 ; y<numlist.size() ; y++){
if(numlist[y]%numlist[x] == 0){
numlist.remove(y);
}
}
} //print list
for(Object item : numlist){
System.out.println((Long)item);
}
}
}

————————————————————————————————————————————————————————————

刚刚去国外网站搜到如下解释:
http://stackoverflow.com/questions/459643/using-a-long-as-arraylist-index-in-java

The bytecode only allows int sized and indexed arrays, so there would have to be a (fairly major) change to the class file format to allow this.

Realize that with a 32-bit signed int index to a long[] you're addressing 16GB of RAM.

The Java specification limits arrays to at most Integer.MAX_VALUE elements. While a List may contain more elements (this is true for Collections in general), you can only add/get/remove/set them using an int index.

Assuming you have the memory for that many elements (very unlikely I think), you could write your own data structure consisting of "concatenated" arrays. The get() and set() methods would take a long index and figure out the corresponding array and int index within that array.

Also, I would suggest using booleans to represent the state of each number, instead of storing/removing each number explicitly. This would be better because (1) booleans take less space than longs, and (2) shifting elements (as done in ArrayList) during element removal can be expensive.

There have been proposals to add long-indexed arrays to Java via Project Coin ( http://mail.openjdk.java.net/pipermail/coin-dev/2009-March/000869.html ) although nothing has been accepted or scheduled.

总结起来就是:
1.字节码不容许
2.用long做脚标需要至少16G内存
3.可以用连接数组自行实现使用long为脚标的数据结构(建议使用一个boolean来替代保存,删除操作)
4.已有建议java使用long脚标数组的提议,未被采用罢了

Using a long as ArrayList index in java的更多相关文章

  1. 数组容器(ArrayList)设计与Java实现,看完这个你不懂ArrayList,你找我!!!

    数组容器(ArrayList)设计与Java实现 本篇文章主要跟大家介绍我们最常使用的一种容器ArrayList.Vector的原理,并且自己使用Java实现自己的数组容器MyArrayList,让自 ...

  2. 从`ArrayList`中了解Java的迭代器

    目录 什么是迭代器 迭代器的设计意义 ArrayList对迭代器的实现 增强for循环和迭代器 参考链接 什么是迭代器 Java中的迭代器--Iterator是一个位于java.util包下的接口,这 ...

  3. ArrayList Iterator remove java.lang.UnsupportedOperationException

    在使用Arrays.asList()后调用add,remove这些method时出现 java.lang.UnsupportedOperationException异常.这是由于Arrays.asLi ...

  4. LeetCode算法题-Find Pivot Index(Java实现)

    这是悦乐书的第304次更新,第323篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第172题(顺位题号是724).给定一个整数nums数组,编写一个返回此数组的" ...

  5. ArrayList遍历(JAVA)

    假如有个ArrayList变量如下: ArrayList<String> list = new ArrayList<String>(); list.add("arra ...

  6. Java 集合系列04之 fail-fast总结(通过ArrayList来说明fail-fast的原理、解决办法)

    概要 前面,我们已经学习了ArrayList.接下来,我们以ArrayList为例,对Iterator的fail-fast机制进行了解.内容包括::1 fail-fast简介2 fail-fast示例 ...

  7. Java集合源码分析(二)ArrayList

    ArrayList简介 ArrayList是基于数组实现的,是一个动态数组,其容量能自动增长,类似于C语言中的动态申请内存,动态增长内存. ArrayList不是线程安全的,只能用在单线程环境下,多线 ...

  8. Java集合系列:-----------06List的总结(LinkedList,ArrayList等使用场景和性能分析)

    现在,我们再回头看看总结一下List.内容包括:第1部分 List概括第2部分 List使用场景第3部分 LinkedList和ArrayList性能差异分析第4部分 Vector和ArrayList ...

  9. Java集合系列:-----------04fail-fast总结(通过ArrayList来说明fail-fast的原理以及解决办法)

    前面,我们已经学习了ArrayList.接下来,我们以ArrayList为例,对Iterator的fail-fast机制进行了解.内容包括::1 fail-fast简介2 fail-fast示例3 f ...

随机推荐

  1. 安装错误 ERROR: DW050: - Microsoft Visual C++ 2010 Redistributable Package (x86): Install failed

    这个问题很可能是你的 Microsoft Visual C++ 2010 Redistributable Package (x86) 太新的缘故,所以无法安装成功,导致最终的失败. 在控制面板-程序和 ...

  2. idea autoscroll from source

  3. HTTP状态码具体解释

    HTTP状态码(HTTP Status Code)是用以表示网页serverHTTP响应状态的3位数字代码.它由 RFC 2616 规范定义的,并得到RFC 2518.RFC 2817.RFC 229 ...

  4. 适合移动手机使用的js环形菜单特效插件

    blooming-menu是一款适合在移动手机上使用的js环形菜单插件.该环形菜单提供了众多的參数,通过结合CSS3动画制作出效果很炫酷的圆形菜单展开和隐藏动画效果. 以下是这个圆形菜单菜价的可用參数 ...

  5. Web.xml 错误或异常页面配置

         <error-page>          <error-code>404</error-code>          <location>/ ...

  6. Unity3.5 GameCenter基础教程(转载)

    原地址: http://forum.unity3d.com/threads/116901-Game-Center-Support/page3 using UnityEngine; using Unit ...

  7. 常见Style 对象属性值

    Style对象的主要需要关注的属性分为4类,下面分别介绍下: Background 属性 backgroundColor 属性设置元素的背景颜色 Object.style.backgroundColo ...

  8. An error occured while handling a json request

    修复方法: sudo pip install werkzeug==0.8.3

  9. Android so文件生成

    http://blog.csdn.net/laczff21/article/details/7542236 http://blog.csdn.net/yhm2046/article/details/8 ...

  10. 【DB2】NULLS LAST与NULLS FIRST

    DB2函数配合 select row_number() over(order by col desc nulls first/nulls last ) as rn,col from table1ord ...