簡體   English   中英

如何將數字放入2D數組中?

[英]How can I put numbers inside a 2D array?

在我的代碼中,該方法能夠讀取.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]

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM