为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

慕的地10843 2023-07-28 16:34:16
在我的代码中,该方法能够读取 .txt 文件并将整数放入数组的一侧,将双精度数放入另一侧。但是,在输出中存在重复项,我试图将它们按升序排列,没有重复项。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");            if (container[0].length == intIndex)             {                container[0] = Arrays.copyOf(container[0], intIndex + 2); //add two more slot to int array                container[1] = Arrays.copyOf(container[1], intIndex + 2); //add two more slot to double array            }            container[0][intIndex] = (text); //add to container            intIndex++; //adjust the index        } else if (text.matches("\\d*.\\d*"))         {            System.out.println(text + " is double");            if (container[1].length == doubleIndex)             {                container[0] = Arrays.copyOf(container[0], doubleIndex + 2); //add two more slot to int array                container[1] = Arrays.copyOf(container[1], doubleIndex + 2); //add two more slot to double array            }            container[1][doubleIndex] = (text); //add to container            doubleIndex++; //adjust the index        } else         {            System.out.println(text + " is not int nor double");        }    }  }.txt 文件将所有内容包含在一行中,“10 5 Five 10 1.5 2 2.0 20”我期望输出为: [2, 10, 20] [1.5, 2.0]然而,我得到的实际输出是: [10, 10, 2, 20] [1.5, 2.0, null, null]
查看完整描述

1 回答

?
达令说

TA贡献1821条经验 获得超6个赞

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("~~~~~~~~~~~~~~~~~~~~~~~~~~~~");

}


查看完整回答
反对 回复 2023-07-28
  • 1 回答
  • 0 关注
  • 62 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信