如何在JAVA中将多个数组合并到一个矩阵中?

时间:2014-11-07 23:10:23

标签: java arrays matrix

有没有一种简单的方法来合并多个,比如char数组来获取char矩阵?我下面有8个数组,每个有64个字符,我想合并到一个包含8行和64列的矩阵。

package august26;

import java.util.Scanner;

public class XBits {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);
    long n1 = input.nextInt();
    long n2 = input.nextInt();
    long n3 = input.nextInt();
    long n4 = input.nextInt();
    long n5 = input.nextInt();
    long n6 = input.nextInt();
    long n7 = input.nextInt();
    long n8 = input.nextInt();

    String s1 = String.format("%64s", Long.toBinaryString(n1)).replace(' ', '0');
    String s2 = String.format("%64s", Long.toBinaryString(n2)).replace(' ', '0');
    String s3 = String.format("%64s", Long.toBinaryString(n3)).replace(' ', '0');
    String s4 = String.format("%64s", Long.toBinaryString(n4)).replace(' ', '0');
    String s5 = String.format("%64s", Long.toBinaryString(n5)).replace(' ', '0');
    String s6 = String.format("%64s", Long.toBinaryString(n6)).replace(' ', '0');
    String s7 = String.format("%64s", Long.toBinaryString(n7)).replace(' ', '0');
    String s8 = String.format("%64s", Long.toBinaryString(n8)).replace(' ', '0');

    s1.toCharArray();
    s2.toCharArray();
    s3.toCharArray();
    s4.toCharArray();
    s5.toCharArray();
    s6.toCharArray();
    s7.toLowerCase();
    s8.toCharArray();
    input.close();
}

}

2 个答案:

答案 0 :(得分:1)

char[][] matrix = new char[8][];
matrix[0] = s1.toCharArray();
matrix[1] = s2.toCharArray();

等...

答案 1 :(得分:0)

未经测试的伪代码:

package august26;

import java.util.Scanner;

public class XBits {
public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    ArrayList<Character[]> charArray = new ArrayList<Char[]>(); 
    while (input.hasNextInt()) {
        charArray.add(
            String
                .format("%64s", Long.toBinaryString(input.nextInt())
                .replace(' ', '0')
                .toCharArray()
        );
    }
    input.close();
}