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. 使用ant构建报错,编码GBK的不可映射字符解决方法

    使用ant的核心就是这个build.xml.然后再在cmd中使用ant命令就行了. build.xml构建文件包含一个工程(project). 工程包含若干个目标(target). 目标可以依赖于其他 ...

  2. ckeditor 前段js配置toolbar以及取值(实用)

    <%@ page contentType="text/html;charset=UTF-8"%><%@ include file="/WEB-INF/v ...

  3. php抽象与接口的区别[转载]

    来自:http://www.cnblogs.com/k5054/archive/2012/12/26/2834205.html 对于面向对象开发,抽象类与接口这两个东西是比较难理解的! 今天看了一整天 ...

  4. [读后感]Java 控制台执行 Jar 传參的编码问题

    下面这版面,太让我纠结了.谁能帮调整一下,... _________________ java -jar test.jar "一二一" 这样传进去的 "一二一" ...

  5. Loadrunner关于页面检查的几个函数详解

    环境:Loadrunner版本:8.0自建一个test.html文件:<html><head><meta name="google1" content ...

  6. Android Exception 12(has leaked ServiceConnection)

    09-09 15:12:31.154: E/ActivityThread(18855): Activity com..xxx.xx.act.LoadingAct has leaked ServiceC ...

  7. 强大的代码生成工具 MyGeneration

    MyGeneration是一个功能很强大的代码生成工具.通过编写包含各种类型脚本(C#,VB.Net,JScript,VBScript)的模板,通过数据库的表内容,生成你需要的各种代码.你可以用它来生 ...

  8. glassfish3操作命令

    glassfish3操作命令   启动:C:\Java\glassfish\bin>asadmin start-domain domain1 停止:C:\Java\glassfish\bin&g ...

  9. MAC 通过brew安装软件

    1.安装brew ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/instal ...

  10. mysql导入慢

    MySQL导出的SQL语句在导入时有可能会非常非常慢,经历过导入仅45万条记录,竟用了近3个小时.在导出时合理使用几个参数,可以大大加快导 入的速度. -e 使用包括几个VALUES列表的多行INSE ...