我是新手,我不知道如何合并我的部分知识。 我必须让算法寻找最长的数字上升序列。 我的文件有 50 列,每列都有一些用空格分隔的随机数。
我仍在尝试将它们放入 Matrix 或其他多维数组中,但我不知道如何操作。
这是我的代码:
import java.io.File;
import java.util.Scanner;
import java.util.StringTokenizer;
public class reading {
public static void main(String[] args) {
double[][] numbers = new double[101][101];
int column = 0, row = 0;
try {
System.out.print("Enter the file name with extension : ");
Scanner input = new Scanner(System.in);
File file = new File(input.nextLine());
input = new Scanner(file);
for (int columns = 0; columns < 101; columns++) {
String input2 = input.nextLine();
StringTokenizer strToken = new StringTokenizer(input2);
int count = strToken.countTokens();
for (int rows = 0; rows < count; rows++) {
numbers[columns][rows] = Integer.parseInt((String) strToken.nextElement());// writing values to arrays
System.out.println(numbers[columns][rows]);// test
}
}
input.close();
} catch (Exception ex) {
ex.printStackTrace();
}
// here someday will be algorithm
}
}
** 抱歉,我的英语不好,我想说出我想要什么,但这对我来说有点困难。 :( 我得到了很多数字的文件,我必须在不替换数字的情况下找到最长的数字系列,从小到大 **
文件中的小例子“
45 -31 -21 -34 30 -2 12 21 -39 -46 -48 8 15 30 8 -48 29 12 11 -28 40 27 28 -45 2 50 8 28 14 47 -22 -20 27 16 43 -7 35 13 7 15 40 -42 23 -7 7 18 22 13 28 -33 -15 -46 12 -22 31 -33 39 34 -11 45 -25 -25 -50 48 31 -20 -25 -5 5 18 -36 -24 -17 10 24 21 -35 6 19 38 6 44 20 30 -49 -33 -44 9 37 -36 -18 2 -2 35 -2 45 -36 40 26 -42 -17 45 40 -31 -21 33 -4 -50 40 13 -50 11 12 37 -26 38 -31 7 30 4 32 -50 - 7 -40 -12 27 17 -5 -11 41 -1 46 16 16 48 38 -49 10 1 25 39 26 -14 -50"
最佳答案
如果您正在尝试读取以空格分隔的值,最简单的解决方案可能就是使用 Scanner.next
或其他 next...
函数之一(nextLine
除外,例如 Scanner.nextInt
或您的情况下的 Scanner.nextDouble
)。
这些函数使用空格作为分隔符,因此每次后续调用都将返回空格之间的下一段文本,并将其转换为适当的类型(如果适用)。
input = new Scanner(file);
for (int rows = 0; rows < numbers.length; rows++){
for (int columns = 0; columns < numbers[rows].length; columns++){
numbers[rows][columns] = input.nextDouble();
}
}
如果你希望文件中的每一行都是一行,rows
循环应该在外面(考虑到你想从第 0 行开始并以相同的方式遍历每一列您将从第 0 行开始并遍历该行中的每个值)。
这不是特别可靠(如果您的数据有问题,它可能仍然有效,或者可能很难找到问题所在)。
如果您正在寻找更强大的东西,我可能会推荐更像这样的东西:
for (int rows = 0; rows < numbers.length; rows++){
String[] split = input.nextLine().split("\\s+"); // \\s+ is 1 or more whitespace characters
if (split.length != numbers.length) {
throw new IllegalArgumentException("Line " + rows + " has " + split.length + " columns, but needs " + numbers.length);
}
for (int columns = 0; columns < numbers[rows].length; columns++){
numbers[rows][columns] = Double.parseDouble(split[columns]);
}
}
在 Java 8 中,我们可以像这样实现相同的结果:
final int DESIRED_ROW_LENGTH = 101;
for (int rows = 0; rows < numbers.length; rows++){
numbers[rows] = Arrays.stream(input.nextLine().split("\\s+"))
.mapToDouble(Double::parseDouble).toArray();
if (numbers[rows].length != DESIRED_ROW_LENGTH) {
throw new IllegalArgumentException("Line " + rows + " has " + numbers[rows].length + " columns, but needs " + DESIRED_ROW_LENGTH);
}
}
关于java - 将由空格分隔的文件中的值放入多维数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48302852/