用.split(“,”)转置数组列表

时间:2012-02-04 22:30:47

标签: java

我有一个在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


}

1 个答案:

答案 0 :(得分:3)

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

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

转置不是必需的,你可以在填充文件数据之后以不同的方式循环输入数组,但我已将它留在那里,以防你真的想要转换输入。