gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 00:43:28 25 4
gpt4 key购买 nike

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

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 5 Five 10 1.5 2 2.0 20”

我希望输出是:[2,10,20][1.5,2.0]

但是,我得到的实际输出是:[10,10,2,20][1.5、2.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("~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
}

关于java - 如何将数字放入二维数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57916902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com