如何将数字放入2D数组中?

时间:2019-09-13 03:22:07

标签: java arrays

在我的代码中,该方法能够读取.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");
        }
    }
  }
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
    Arrays.sort(container[0], Comparator.nullsLast(Comparator.naturalOrder())); //sort array of int
    Arrays.sort(container[1], Comparator.nullsLast(Comparator.naturalOrder())); //sort array of double
    System.out.println(Arrays.toString(container[0]));
    System.out.println(Arrays.toString(container[1]));
    System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  }

.txt文件将所有内容都包含在一行中,“ 10 5five 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 个答案:

答案 0 :(得分:0)

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("~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}
相关问题