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 ...
随机推荐
- (转)NIO 分散和聚集
分散和聚集 概述 分散/聚集 I/O 是使用多个而不是单个缓冲区来保存数据的读写方法. 一个分散的读取就像一个常规通道读取,只不过它是将数据读到一个缓冲区数组中而不是读到单个缓冲区中.同样地,一个聚集 ...
- 一个Tomcat配置参数引发的血案
转载:https://mp.weixin.qq.com/s/3IuTcDCTB3yIovp6o_vuKA 一.现象 有用户反馈访问PC首页偶尔会出现白页情况,也偶尔会收到听云的报警短信 二.监控(听云 ...
- redis学习笔记——初始化
初始化服务器状态结构 redis中一个最重要的数据结构是redis_server,会创建一个这个结构的全局变量server,初始化服务器的第一步就是创建一个struct redisServer类型的实 ...
- EXCEL在使用中,jar导入问题
报错:The type org.apache.poi.ss.usermodel.Workbook cannot be resolved. It is indirectly referenced fro ...
- git commit 出现 changes not staged for commit 错误
git commit 出现 changes not staged for commit 错误 修复: 参考:http://stackoverflow.com/questions/8488887/git ...
- Android中Native和H5交互
1.概述 时至今日,H5的跨平台性越发凸显优势,一套代码适配android.ios,既能减少开发成本,又便于更新与维护.但是native的性能体验也确实更佳,尤其体现在复杂界面和频繁变化的界面上.事实 ...
- python——修饰符
修饰符基础--闭包 什么是闭包呢?标准的概念大家可以看wikipedia上的解释 举个例子: def do_add(base): def add(increase): return base + in ...
- stylus
stylus介绍 是个什么鬼?对于开发来说,CSS的弱点在于静态化.我们需要一个真正能提高开发效率的工具, LESS , SASS 都在这方面做了一些贡献. Stylus 是一个CSS的预处理框架,2 ...
- Android4.4 SystemUI加入Dialog弹窗
此弹窗为开机SystemUI的显示弹窗: 首先.在SystemUI的源代码文件夹加入源代码类文件,文件夹为frameworks/base/packages/SystemUI/src/com/andro ...
- [1-1] 把时间当做朋友(李笑来)Chapter 1 【心智的力量】 摘录
今天开了读书笔记这一专题,主要是对自己今后读的书有一个小小的记录,也为解决自己读书多年的存在的一些习惯的问题. 打小就喜欢书,可能最早的书是家人买的看图识动物.还记得七八岁时见书摊上的书时赖着不走央求 ...