我想将文本的双值放在2d数组上。我的文本有双重和字符串值
我的文字
deneme deneme 4.5 5.5 6.6 7.5 9.9 9.9
dsdsa sddsf 9.9 8.7 7.5 8.8 4.4 6.5
来自字符串的extraxt double值。 我的节目输出
4.5 5.5 6.6 7.5 9.9 9.9 9.9 8.7 7.5 8.8 4.4 6.5
怎么能做我的输出六重奏。如果ı能做到这一点,ı将对每一行进行排序
第一线:4.5 5.5 6.6 7.5 9.9 9.9
第二行:9.9 8.7 7.5 8.8 4.4 6.5
public static void main(String[] args) throws IOException {
ArrayList<String> puan;
puan = okuDiziyeKoy("C:\\deneme\\HW.txt");
System.out.format("%d kayıt okundu.%n", puan.size());
for (int j = 0; j < puan.size(); j++) {
String point = puan.get(j);
String[] edit = point.split("[\\{\\}\\)]");
for (String s : edit) {
String a = s.replaceAll("[\\}\\-\\>\\>\\]\\[\\#\\*\\{]+", "");
String b = a.replaceAll("[\\.]+", ",");
Scanner scanner = new Scanner(b);
while (scanner.hasNext()) {
if (scanner.hasNextDouble()) {
Double doubleValue = scanner.nextDouble();
List<Double> numbers = new ArrayList<Double>();
numbers.add(doubleValue);
// System.out.println(Arrays.deepToString(numbers.toArray()));
final int col = 6;
final int rows = numbers.size() % col == 0 ? numbers.size() / col : numbers.size() / col + 1;
double[][] deneme = new double[rows][col];
for (int i = 0; i < rows; i++) {
for (int k = 0; k < col; k++) {
if (i == rows - 1 && k >= numbers.size() % col)
break;
deneme[i][k] = numbers.get(k + col * i);
System.out.print( deneme[i][k]+ " ");
}// for k end
System.out.println(); // go to next line
} // for i end
}// if scan end
else {
//if it is string comes value here
String stringValue = scanner.next();
}// if scan end
}// while end
}// for string s
}// for end
}// main end
答案 0 :(得分:2)
在内部循环(k
循环)之后引入换行符,使用System.out.println()
。
for (int i = 0; i < numbers.size(); i++) {
for (int k = 0; k < numbers.size(); k++) {
deneme[i][k] = numbers.get(k + (numbers.size() * i));
System.out.print( deneme[i][k]+ " ");
}// for k end
System.out.println(); // to introduce a newline
} // for i end
答案 1 :(得分:1)
将元素还原到deneme
时,由于deneme[0]
从k
到0
,所有元素都会被放入numbers.size() - 1
。您可以修改如下:
final int col = 6;
final int rows = numbers.size() % col == 0 ? numbers.size() / col : numbers.size() / col + 1;
double[][] deneme = new double[rows][col];
for (int i = 0; i < rows; i++) {
for (int k = 0; k < col; k++) {
if (i == rows - 1 && k >= numbers.size() % col) // UPDATE: last line may not contain `col` sized elements
break;
deneme[i][k] = numbers.get(k + col * i);
System.out.print( deneme[i][k]+ " ");
}// for k end
System.out.println(); // go to next line
} // for i end