Using a long as ArrayList index in java
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的更多相关文章
- 数组容器(ArrayList)设计与Java实现,看完这个你不懂ArrayList,你找我!!!
数组容器(ArrayList)设计与Java实现 本篇文章主要跟大家介绍我们最常使用的一种容器ArrayList.Vector的原理,并且自己使用Java实现自己的数组容器MyArrayList,让自 ...
- 从`ArrayList`中了解Java的迭代器
目录 什么是迭代器 迭代器的设计意义 ArrayList对迭代器的实现 增强for循环和迭代器 参考链接 什么是迭代器 Java中的迭代器--Iterator是一个位于java.util包下的接口,这 ...
- ArrayList Iterator remove java.lang.UnsupportedOperationException
在使用Arrays.asList()后调用add,remove这些method时出现 java.lang.UnsupportedOperationException异常.这是由于Arrays.asLi ...
- LeetCode算法题-Find Pivot Index(Java实现)
这是悦乐书的第304次更新,第323篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第172题(顺位题号是724).给定一个整数nums数组,编写一个返回此数组的" ...
- ArrayList遍历(JAVA)
假如有个ArrayList变量如下: ArrayList<String> list = new ArrayList<String>(); list.add("arra ...
- Java 集合系列04之 fail-fast总结(通过ArrayList来说明fail-fast的原理、解决办法)
概要 前面,我们已经学习了ArrayList.接下来,我们以ArrayList为例,对Iterator的fail-fast机制进行了解.内容包括::1 fail-fast简介2 fail-fast示例 ...
- Java集合源码分析(二)ArrayList
ArrayList简介 ArrayList是基于数组实现的,是一个动态数组,其容量能自动增长,类似于C语言中的动态申请内存,动态增长内存. ArrayList不是线程安全的,只能用在单线程环境下,多线 ...
- Java集合系列:-----------06List的总结(LinkedList,ArrayList等使用场景和性能分析)
现在,我们再回头看看总结一下List.内容包括:第1部分 List概括第2部分 List使用场景第3部分 LinkedList和ArrayList性能差异分析第4部分 Vector和ArrayList ...
- Java集合系列:-----------04fail-fast总结(通过ArrayList来说明fail-fast的原理以及解决办法)
前面,我们已经学习了ArrayList.接下来,我们以ArrayList为例,对Iterator的fail-fast机制进行了解.内容包括::1 fail-fast简介2 fail-fast示例3 f ...
随机推荐
- Fedora 中的容器技术:systemd-nspawn
本文将说明你可以怎样使用 Fedora 中各种可用的容器技术和学习“systemd-nspawn”的相关知识. 容器是什么? 一个容器就是一个用户空间实例,它能够在与托管容器的系统(叫做宿主系统)相隔 ...
- iOS 8 Share Extension Safari URL Example(在iOS中分享url的样例)
ios8 的Extension给我们提供了非常多奇妙的功能.以后分享内容再也不用进入app了,让我们的手机更安全,以下我们以在safari 浏览器中分享一个web url 来讲述Share Exten ...
- PHP的抽象类、接口的区别和选择[转载]
区别: 1.对接口的使用是通过关键字implements.对抽象类的使用是通过关键字extends.当然接口也可以通过关键字extends继承. 2.接口中不可以声明成员变量(包括类静态变量),但是可 ...
- windows系统中常用的快捷键
开发中,一些常用的键盘技巧: (1)快速切换桌面(alt+tab)或者是(window+tab) (2)迅速关闭当前页面 (3)迅速收起所有页面 (4)直接回到桌面页面 (5)锁定你的屏幕
- 在linux下的使用复制命令cp,不让出现“overwrite”(文件覆盖)提示的方法。(转)
在linux下的使用复制命令cp,不让出现“overwrite”(文件覆盖)提示的方法. 一般我们在使用cp命令时加上-f选项,希望不让出现“overwrite”的提示(文件覆盖的提示).如:# cp ...
- Unity 开发游戏编写代码的技巧
在平时开发游戏过程中,遇到一些编写代码很繁琐的问题. 我发现我团队中每个人都会遇到,就算打写出来分享下经验. 条件断点 利用IDE提供的工具, 右键断点的时候 输入条件, 当条件达成的时候,断点才能命 ...
- 使用Monkeyrunner进行Android自动化的总结
http://www.2cto.com/kf/201411/356056.html 使用Monkeyrunner进行Android自动化的总结 使用Android自动化的方式,不仅可以用来对Andro ...
- log4j 配置(转)
log4j是干什么的 log4j是Apache的一个开源项目,主要功能是打印日志信息,以各种形式在各种地方花式打印日志. 使用log4j的准备工作 使用log4j就必须要引入其jar包.附上官网地址h ...
- iDempiere VS ADempiere
怀揣着为中小企业量身定做一整套开源软件解决方案的梦想开始了一个网站的搭建.http://osssme.org/ 第三篇:iDempiere VS ADempiere 一直以来,什么谁谁谁VS谁谁谁的, ...
- Sphinx-PHP使用Sphinx搜索技术
Sphinx继承到PHP程序中, 有两种方式: Sphinx PHP模块: 编译生成PHP扩展模块 Sphinx API类: 直接使用Sphinx提供的类即可 首先我们应该使用Sphinx做以下几件事 ...