Java 如何将数字放入二维数组中?

Java 如何将数字放入二维数组中?,java,arrays,Java,Arrays,在我的代码中,该方法能够读取.txt文件,并将整数放在数组的一侧,将双精度放在另一侧。但是,在输出中有重复项,我尝试将它们按升序排列,没有重复项 publicstaticvoidreadfile(字符串文件)抛出FileNotFoundException { 扫描仪s1=新扫描仪(新文件(文件)); 字符串[][]容器=新字符串[2][2]; int intIndex=0; int-doubleIndex=0; while(s1.hasNextLine()) { 字符串行=s1.nextLine

在我的代码中,该方法能够读取.txt文件,并将整数放在数组的一侧,将双精度放在另一侧。但是,在输出中有重复项,我尝试将它们按升序排列,没有重复项

publicstaticvoidreadfile(字符串文件)抛出FileNotFoundException
{
扫描仪s1=新扫描仪(新文件(文件));
字符串[][]容器=新字符串[2][2];
int intIndex=0;
int-doubleIndex=0;
while(s1.hasNextLine())
{
字符串行=s1.nextLine();
系统输出打印项次(行);
System.out.println(“~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~”;
String[]splitLine=line.split(“”);
用于(字符串文本:拆分行){
如果(文本匹配(\\d*))
{
System.out.println(text+“is int”);
if(容器[0]。长度==intIndex)
{
container[0]=array.copyOf(container[0],intIndex+2);//向int数组再添加两个插槽
container[1]=Arrays.copyOf(container[1],intIndex+2);//向双数组再添加两个插槽
}
容器[0][intIndex]=(文本);//添加到容器
intIndex++;//调整索引
}else if(text.matches(“\\d*\\d*”)
{
System.out.println(text+“是双精度”);
if(容器[1]。长度==双索引)
{
container[0]=Arrays.copyOf(container[0],doubleIndex+2);//向int数组再添加两个插槽
container[1]=Arrays.copyOf(container[1],doubleIndex+2);//向double数组再添加两个插槽
}
容器[1][doubleIndex]=(文本);//添加到容器
doubleIndex++;//调整索引
}否则
{
System.out.println(text+“既不是int也不是double”);
}
}
}
System.out.println(“~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~”;
Arrays.sort(容器[0],Comparator.nullsLast(Comparator.naturalOrder());//对int的数组进行排序
Arrays.sort(容器[1],Comparator.nullsLast(Comparator.naturalOrder());//对double的数组进行排序
System.out.println(Arrays.toString(容器[0]);
System.out.println(Arrays.toString(容器[1]);
System.out.println(“~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~”;
}
txt文件在一行中包含了这一切,“1055 10 1.5 2 2.0 20”

我预计产出为: [2, 10, 20] [1.5,2.0]

然而,我得到的实际输出是: [10, 10, 2, 20]
[1.5、2.0、null、null]

是否有理由将数组大小增加2?这将导致输出中可能出现空值。如果您只需要类似于
[2,10,20]
的内容,则必须将数组大小增加1而不是2。如果任一行被填充,则应将数组替换为大小为2 x4的数组,并复制已读取的所有数字。如果任何一行再次被填充,它应该用一个大小为(2x8)的数组替换数组,并复制所有已经读取的数字。我的意思是,您需要双数组和int数组具有相同的长度吗?如果不需要它们的长度相同,则可以通过仅将要添加到的数组增加1来完全避免空值。为什么是这样,我希望它们的长度相同,但是,我如何处理输出中的重复项并按升序排列?分解您的更改或在巨大代码块之外解释您的修复将更有效。@Bakna我在更改/添加的行周围添加了一些空格和注释
public static void readFile(String file) throws FileNotFoundException
{
    Scanner s1 = new Scanner(new File(file));
    String[][] container = new String[2][2];
    int intIndex = 0;
    int doubleIndex = 0;
    while(s1.hasNextLine())
    {
        String line = s1.nextLine();
        System.out.println(line);
        System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        String[] splitLine = line.split(" ");
        for (String text : splitLine) {
            if (text.matches("\\d*")) 
            {
                System.out.println(text + " is int");

                //checking the array for duplicates
                if (Arrays.stream(container[0]).anyMatch(text::equals)) {
                    continue;
                }


                if (container[0].length == intIndex) 
                {
                    container[0] = Arrays.copyOf(container[0], intIndex + 2); 
                    container[1] = Arrays.copyOf(container[1], doubleIndex + 2); 
                }
                container[0][intIndex] = (text); 
                intIndex++; //adjust the index
            } else if (text.matches("\\d*.\\d*")) 
            {
                System.out.println(text + " is double");

                //checking the array for duplicates
                if (Arrays.stream(container[1]).anyMatch(text::equals)) {
                    continue;
                }


                if (container[1].length == doubleIndex) 
                {
                    container[0] = Arrays.copyOf(container[0], intIndex + 2); 
                    container[1] = Arrays.copyOf(container[1], doubleIndex + 2); 
                }
                container[1][doubleIndex] = (text); 
                doubleIndex++; 
            } else 
            {
                System.out.println(text + " is not int nor double");
            }
        }
    }
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

    //compare numerically rather than alphabetically in sorting
    Arrays.sort(container[0], Comparator.nullsLast((e1, e2) -> 
            Integer.valueOf(e1).compareTo(Integer.valueOf(e2))));
    Arrays.sort(container[1], Comparator.nullsLast((e1, e2) -> 
            Double.valueOf(e1).compareTo(Double.valueOf(e2))));


    System.out.println(Arrays.toString(container[0]));
    System.out.println(Arrays.toString(container[1]));
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}