java - 从文本文件获取输入时不比较大小

标签 java arrays arraylist

我有以下代码。

import java.io.File;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Scanner;

public class Scalar {
    public static void main(String args[]) throws Exception {

        Scanner sc=new Scanner(new File("D:/GC/Scalar/A-small.in"));
        int testcases=Integer.parseInt(sc.next());
        System.out.println(testcases);
        ArrayList<BigInteger> a=new ArrayList<BigInteger>();
        ArrayList<BigInteger> b=new ArrayList<BigInteger>();

        for(int j=0;j<2;j++){

        int size=Integer.parseInt(sc.next());
        System.out.println(size);

        for(int i=0;i<size;i++)
        {
            a.add(sc.nextBigInteger());

        }


        for(int i=0;i<size;i++)
        {
            b.add(sc.nextBigInteger());

        }
        Collections.sort(a);        
        System.out.println(a.size());
        System.out.println(a);
        Collections.sort(b,Collections.reverseOrder());
        System.out.println(b);
        BigInteger sum;
        for(int i=0;i<a.size();i++){
            sum=a.get(i).multiply(b.get(i));
            sum=sum.add(sum);

        }

    }

    }


}

以及文本文件中的以下内容。

1000
3
1 -5 3
-2 1 4
5
5 4 3 1 2
1 1 0 1 0
7
677 463 -569 516 401 -998 882
890 588 959 909 948 -617 -655
8
-912 937 167 366 -222 -397 190 -216
354 

这里我尝试按相反顺序对第一个数组和第二个数组进行排序,然后进行求和和乘积,这里我只采用了 2 个案例,上面输入中的 1000 是测试案例的总数,以及单数行表示数组大小,在我的程序中,为了确保数组的大小与给定的大小匹配,我正在打印大小,在第一种情况下,输入的大小为 3,我得到了正确的结果,但在第二种情况,输入的大小为 5,但我得到的数组大小为 8,下面是我得到的输出。

1000
3
3
[-5, 1, 3]
[4, 1, -2]
5
8
[-5, 1, 1, 2, 3, 3, 4, 5]
[4, 1, 1, 1, 1, 0, 0, -2]

请让我知道我哪里出错了。

谢谢

最佳答案

这就是问题:

ArrayList<BigInteger> a=new ArrayList<BigInteger>();
ArrayList<BigInteger> b=new ArrayList<BigInteger>();

for(int j=0;j<2;j++){
    // Stuff
}

您为每个测试重复使用相同的列表 - 因此您将第二个测试的值添加到已包含第一个测试的数据的列表中。

选项:

  • (首选)为每个测试创建新列表
  • 在每次测试开始时清除列表

鉴于您逻辑上不想将列表从一个测试保留到另一个测试,我只需将代码更改为:

for(int j = 0; j < 2; j++){
    List<BigInteger> a = new ArrayList<BigInteger>();
    List<BigInteger> b = new ArrayList<BigInteger>();
    // Populate the lists, etc.
}

请注意,这可以更轻松地将“读取大小、读取数据、排序”的整个操作提取到单独的方法中。

此外,我怀疑您想将循环更改为:

for(int j = 0; j < testcases; j++) {

...否则你会在两次测试后停止。

如果您使用的是 Java 7,您也可以使用类型推断来简化 ArrayList 创建过程:

List<BigInteger> a = new ArrayList<>();

...您甚至可以考虑将“将 n 个数字读取到列表中”提取到一个单独的方法中,以便您的代码看起来像这样:

int size = Integer.parseInt(sc.next());
System.out.println(size);
List<BigInteger> a = readBigIntegers(sc, size);
List<BigInteger> b = readBigIntegers(sc, size);
// Now sort etc.

还可以考虑使用 Scanner.nextInt() 而不是显式的 Integer.parseInt 调用。

关于java - 从文本文件获取输入时不比较大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22007696/

相关文章:

java - 我应该如何测试接口(interface)?

java - VpnService.java中的 'protect'方法到底是做什么的

java - 哪种方法更好地比较方法检查中的值或检查它是否存在于数组中?

java - 如何将多个文本字段添加到数组列表 JavaFX

java - 如何从ArrayList的开头删除0

java - 按钮仅在触摸后改变颜色

java - 双向导航。这是什么意思?

c - 指针错误

javascript - 如何使用多个分隔符拆分字符串,这些分隔符在 JavaScript 中仅充当一个分隔符?

java - 用于比较数组的所有字符串值的循环