Java 如何将几个数组的相同索引值存储到单个数组中
所需帮助: 现在我需要将第1行中的所有字母表存储到单独的数组中。Java 如何将几个数组的相同索引值存储到单个数组中,java,arrays,Java,Arrays,所需帮助: 现在我需要将第1行中的所有字母表存储到单独的数组中。 即第1行[]={S,G,M}和第2行[]={T,H,N}等等。。。。。。。。。。。如果我理解你的问题,那么一个可能的解决方案就是用一个二维数组替换你的a、b、c、d和e数组。而且,你只需要一个。你大概可以使用 Scanner Scanner=新的扫描仪(System.in); int y=scanner.nextInt(); char[][]行={{{'A','B','C','D','E','F'}, {'G','H','I','
即第1行[]={S,G,M}和第2行[]={T,H,N}等等。。。。。。。。。。。如果我理解你的问题,那么一个可能的解决方案就是用一个二维数组替换你的
a
、b
、c
、d
和e
数组。而且,你只需要一个。你大概可以使用
Scanner Scanner=新的扫描仪(System.in);
int y=scanner.nextInt();
char[][]行={{{'A','B','C','D','E','F'},
{'G','H','I','J','K','L'},
{'M','N','O','P','Q','R'},
{'S','T','U','V','W','X'},
{'Y','Z','.','.','.','.'.'.};
字符[][]out=新字符[3][y];
for(int i=0;i
你到底想要什么?字符[]数组=新字符[]{'S','G','M'}
!?我想动态地做,而不是直接分配
import java.util.Scanner;
public class namefinder {
public static void main(String[] args) {
System.out.println(" NAME PREDICTER ");
System.out.println(" Row 1 A B C D E F");
System.out.println(" Row 2 G H I J K L");
System.out.println(" Row 3 M N O P Q R");
System.out.println(" Row 4 S T U V W X");
System.out.println(" Row 5 Y Z . . . .");
System.out.println("enter the length of your first name in number!!! EX: vino , so length is 4");
Scanner scanner = new Scanner(System.in);
int y = scanner.nextInt();
int s[] = new int [20];
for(int i=1;i<=y;i++)
{
System.out.println("Enter whether the "+i+"letter of your name is in which row");
Scanner scanner1 = new Scanner(System.in);
s[i] = scanner1.nextInt();
}
char a[] = {'A','B','C','D','E','F'};
char b[] = {'G','H','I','J','K','L'};
char c[] = {'M','N','O','P','Q','R'};
char d[] = {'S','T','U','V','W','X'};
char e[] = {'Y','Z','.','.','.','.'};
for(int i=0;i<6;i++){
for(int j = 0; j <=y; j++) {
switch(s[j] ) { // Since counting starts from 0.
case 1: System.out.print("\t"+a[i]);break;
case 2: System.out.print("\t"+b[i]); break;
case 3: System.out.print("\t"+c[i]); break;
case 4: System.out.print("\t"+d[i]); break;
case 5: try {
System.out.print("\t"+e[i]);
} catch(ArrayIndexOutOfBoundsException ex) {
System.out.println("\t "); // Print an empty space with a \t character.
}
break; // Prone to ArrayIndexOutOfBoundsException
}
}
System.out.println(" "+"Row"+ (i+1)); // Print a newline character after printing every line
}
Scanner scanner = new Scanner(System.in);
int y = scanner.nextInt();
char[][] rows = { { 'A', 'B', 'C', 'D', 'E', 'F' },
{ 'G', 'H', 'I', 'J', 'K', 'L' },
{ 'M', 'N', 'O', 'P', 'Q', 'R' },
{ 'S', 'T', 'U', 'V', 'W', 'X' },
{ 'Y', 'Z', '.', '.', '.', '.' } };
char[][] out = new char[3][y];
for (int i = 0; i < y; i++) {
System.out.println("Enter the row the " + (1 + i)
+ " letter is on:");
int row = scanner.nextInt();
out[i] = Arrays.copyOf(rows[row-1], rows[row-1].length);
}
System.out.println(Arrays.deepToString(out));