【问题标题】:How to store number of elements exceeding the range of integers如何存储超出整数范围的元素数量
【发布时间】:2014-09-30 22:55:09
【问题描述】:

我遇到了一个问题,我必须将 N 个数字的列表存储在一个数组中,然后对其进行排序,
然后我必须在其他位置添加数字并输出总和。
问题是 N 的约束,即 0 11 所以我必须将 N 声明为 double 类型变量这里是我的代码:

ArrayList<Double> myList = new ArrayList<Double>();
myList.add(number);
.....
Collections.sort(myList);
String tempNo = "";
for(double i = 0 ; i < myList.size() ; i=i+2){
tempNo = myStringWayToAdd(tempNo , myList(i)+"");  // Since the sum will exceed the limit of double I have to add the numbers by help of Strings
}

问题get(int)方法采用int而不是double。有没有其他方法可以解决问题? ,并且是否允许存储超过int范围的元素数量?
任何帮助将不胜感激。先感谢您。


编辑 1: 我可以在ArrayList 中使用字符串而不是double,然后将数字相加,但我的问题是我需要存储可能超出整数范围的 N 个元素

【问题讨论】:

  • get(int) 用于获取数组列表中元素的特定索引。 get(0) 获取第一个元素。
  • 据记载,您可以存储多个 Integer.MAX_VALUE 值。顺便说一句,要存储 20 亿个 Double 对象,您至少需要 64 GB 的内存。
  • 祝您在内存中处理超过 20 亿个对象时一切顺利。
  • @Compass 是的,我想在备用索引处获取号码并添加它们
  • @user3921830 啊哈!我会写出来:3

标签: java arraylist


【解决方案1】:

您可以使用LinkedList,因为它是does not have a size limit(尽管那里可能会开始发生奇怪的事情)。如果数字本身可能会变得很大(您似乎没有说明),您还应该能够将 BigInteger 用于您的数字。

    // LinkedList can hold more than Integer.MAX_VALUE elements,
    List<BigInteger> myList = new LinkedList<>();
    // Fill it with a few numbers.
    Random r = new Random();
    for (int i = 0; i < 1000; i++) {
        myList.add(BigInteger.probablePrime(10, r));
    }
    // Sort it - not sure if Collections.sort can handle > Integer.MAX_VALUE elements but worth a try.
    Collections.sort(myList);
    // Start at 0.
    BigInteger sum = BigInteger.ZERO;
    // Add  every other one.
    boolean addIt = false;
    for (BigInteger b : myList) {
        if (addIt) {
            sum = sum.add(b);
        }
        addIt = !addIt;
    }

我不确定Collections.sort是否可以处理这么大的数字列表,更不用说它是否能在宇宙年龄内成功排序。

您可能更愿意考虑使用数据库,但您甚至可能会遇到具有这么多数字的问题。

【讨论】:

  • 他不想要一个包含大值的数组,他想要一个包含值的大数组。
  • @brimborium - 这两者都提供 - OP 似乎想要存储双打并使用一种奇怪的技巧来添加它们,所以也许这就是想要的。
【解决方案2】:

啊,我误解了这个问题。所以我们必须存储比 int 容量大得多的东西。

好吧,我们可以通过分而治之来做到这一点。假设这是理论上的并且我们有无限的内存,我们可以创建一个 SuperArrayList。

'怪我的泛型不好,没有编译器。

public class SuperArrayList<E>() {
    private ArrayList<ArrayList<E>> myList;
    private int squareRoot;
    private double capacity;
    public SuperArrayList(double capacity) {
        this.capacity = capacity;
        squareRoot = Math.ceil(Math.sqrt(capacity)); //we create a 2d array that stores the capacity
        myList = new ArrayList<ArrayList<E>>(); 
        for(int i = 0; i < squareRoot; i++) {
            myList.add(new ArrayList<E>());
        }
    }

    public E get(double index) {
        if(index >= capacity || index < 0) {
            //throw an error
        }
        else {
            return myList.get((int) capacity / squareRoot).get(capacity % squareRoot);
        }       
    }
}

作为 squareRoot 的替代方案,我们可以使用 maxValue 并添加其他长度为 maxvalue 的数组列表。

【讨论】:

  • 我的问题是,如果我要存储 N 超出整数范围的 N 个元素,而不是如何获取第 i 个元素
  • 我认为ArrayList的ArrayList是不允许的?
  • @user3921830 我正在编写一个封装这个的类,给我几分钟时间。但是你可以有一个包含 arraylist 的 arraylist
  • 感谢您的时间,+1
【解决方案3】:
boolean add = true;
for (Double doubleNum : myList) {
    if (add) {   
        tempNo = myStringWayToAdd(tempNo , doubleNum+"");
    }
   add = !add;
}

使用这种方式,您将不必使用索引。

【讨论】:

  • 我在那儿做什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-03-22
  • 1970-01-01
  • 1970-01-01
  • 2017-04-27
  • 1970-01-01
  • 2023-03-11
  • 2010-11-03
相关资源
最近更新 更多