user3626180
user3626180

Reputation: 57

How to Merge multiple arrays into a matrix in JAVA?

Is there an easy way to merge multiple, say char arrays to get a char matrix? I have 8 arrays below with 64 chars each and I want to merge to a matrix with 8 rows and 64 cols.

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

}

Upvotes: 0

Views: 1302

Answers (2)

josiah
josiah

Reputation: 1414

untested pseudocode:

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

Upvotes: 0

TubaBen
TubaBen

Reputation: 196

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

etc...

Upvotes: 2

Related Questions