gpt4 book ai didi

java - 使用 .split 转置 arrayList (",")

转载 作者:行者123 更新时间:2023-12-02 00:30:45 24 4
gpt4 key购买 nike

我有一个程序读取 arrayLists 并转置它。输入文件是
C0 F0 D0 E0 G0 B0
C1 F1 D1 E1 G1 B1
C2 F2 D2 E2 G2 B2
C3 F3 D3 E3 G3 B3
C4 F4 D4 E4 G4 B4
C5 F5 D5 E5 G5 B5
我想知道如何更改程序以使输入为
C0,F0,D0,E0,G0,B0
C1,F1,D1,E1,G1,B1
C2,F2,D2,E2,G2,B2
C3、F3、D3、E3、G3、B3
C4、F4、D4、E4、G4、B4
C5、F5、D5、E5、G5、B5
因此必须在某个地方使用 .split(",") 。我还想知道如何使程序在一般数量的列中读取。那么二维数组列表必须如何使用呢?代码如下:

import java.util.*; 
import java.lang.*;
import java.io.*;

public class list {

public static void main(String[] args) {

FileOutputStream out;
PrintStream p;

try
{
File file = new File(args[0]);
String fileName= file.getName();
int len = fileName.length();
StringBuffer sb = new StringBuffer(fileName);
sb.replace(len-4,len,".hr");
out = new FileOutputStream(sb.toString());
p = new PrintStream( out );


try {
List<String> column1 = new ArrayList<String>();
List<String> column2 = new ArrayList<String>();
List<String> column3 = new ArrayList<String>();
List<String> column4 = new ArrayList<String>();
List<String> column5 = new ArrayList<String>();
List<String> column6 = new ArrayList<String>();

Scanner myfile = new Scanner(new DataInputStream(new FileInputStream(args[0])));

while (myfile.hasNext()) {

column1.add(myfile.next());
column2.add(myfile.next());
column3.add(myfile.next());
column4.add(myfile.next());
column5.add(myfile.next());
column6.add(myfile.next());
}

myfile.close();

p.println(column1);
p.println(column2);
p.println(column3);
p.println(column4);
p.println(column5);
p.println(column6);


} catch (Exception e) {
e.printStackTrace();
}


}
catch (Exception e)
{
System.err.println ("Error writing to file");
}

} // End of MAIN


}

最佳答案

您尝试做的事情不称为转置 - 转置会将行更改为列,反之亦然。看起来您只是想将列提取到一维数组中。像这样的事情应该可以做到:

    public static String[] transpose(String [][] a) {
int r = a.length;
int c = a[r - 1].length;

String [] t = new String[r * c];
for(int i = 0; i < c; ++i) {
for(int j = 0; j < r; ++j) {
t[i * r + j] = a[j][i];
}
}
return t;
}

请注意我如何格式化我的代码。

编辑:

我明白你现在想用转置做什么。无需过多更改代码,这应该可以工作:

import java.io.*;
import java.util.*;


public class SwapIO {

public static String[][] transpose(String [][] a) {
int r = a.length;
int c = a[0].length;

String [][] t = new String[c][r];
for(int i = 0; i < r; i++) {
for(int j = 0; j < c; j++) {
t[j][i] = a[i][j];
}
}
return t;
}

public static void main(String[] args) {
// TODO Auto-generated method stub
FileOutputStream out; // declare a file output object
PrintStream p; // declare a print stream object

try
{
// Create a new file output stream
// connected to "myfile.txt"
File file = new File(args[0]);
String fileName= file.getName();
int len = fileName.length();
StringBuffer sb = new StringBuffer(fileName);
sb.replace(len-4,len,".hr");
out = new FileOutputStream(sb.toString());

// Connect print stream to the output stream
p = new PrintStream( out );

// p.println ("This is written to a file");
Scanner sc = null;
try {
sc = new Scanner(new DataInputStream(new FileInputStream(args[0])));
} catch (FileNotFoundException e) {
e.printStackTrace();
}

try {
// Scanner input = new Scanner(new File("array.txt"));
Scanner input = new Scanner(new DataInputStream(new FileInputStream(args[0])));
int m = count(args[0]) + 1;
int n = count(args[0]) + 1;
int place;
int string;
String[][] a = new String[m][n];
// while (input.hasNextLine()) {

int row = 0;
while (input.hasNextLine()) {
String txt = input.next();
String[] tmp = txt.split(",");
a[row++] = tmp;
}

String[][] transpose = transpose(a);

for (int i = 0; i < m; i++) {
for (int j = 0; j < n; j++) {
try{// System.out.println("number is ");
System.out.println(transpose[i][j]);
}
catch (java.util.NoSuchElementException e) {
// e.printStackTrace();
}
}
}
// }
} catch (Exception e) {
e.printStackTrace();
}
}
catch (Exception e)
{
System.err.println ("Error writing to file");
}
}

public static int count(String filename) throws IOException {
InputStream is = new BufferedInputStream(new FileInputStream(filename));
try {
byte[] c = new byte[1024];
int count = 0;
int readChars = 0;
while ((readChars = is.read(c)) != -1) {
for (int i = 0; i < readChars; ++i) {
if (c[i] == '\n')
++count;
}
}
return count;
} finally {
is.close();
}
}
}

转置并不是真正必要的,您可以在用文件数据填充输入数组后以不同的方式循环遍历输入数组,但我将其留在那里,以防您实际上希望输入转置。

关于java - 使用 .split 转置 arrayList (","),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9145188/

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