从二维字符数组中获取一定数量的字符
getting a certain number of characters from a 2D char array
我有一个二维字符数组,我想打印每个嵌套数组中每五个字符的前三个字符。这就是我正在做的事情:
char [][] one ={ {'a','b','c','d','e','f','g','h','3'},{'i','j','k','l','m','n','o','p','7'},{'q','r','s','t','u','v','w','x','2'}};
int aSize=5;
char [] firstThree=new char[3];
for (int i=0; i< one.length;i++){
for (int j=0; j< aSize;j++){
for(int m=0; m<3;m++){
firstThree[m]=one[i][m];
}
}
System.out.print(firstThree);
System.out.println("");
}
这给出了以下输出:
abc
ijk
qrs
我想要输出:
abc
fgh
ijk
nop
qrs
vwx
你可以这样做:
for (int j=5; j < 8;j++){
firstThree[j-5]=one[i][j];
}
您不需要 3 个循环来执行此操作...
您可以创建另一个数组以在 5 个索引之后读取,例如:
for(int m=0; m<3;m++){
firstThree[m]=one[i][m];
nextThree[m]=one[i+5][m];
}
这可以动态地进行,因此它可以用于更长的输入长度。
请注意,第三个嵌套循环不是必需的,您只需要完全遍历每个嵌套数组,使用 count
变量来跟踪添加前三个数组的位置每五个字符。
请注意,我还使用了一个 ArrayList<String>
来跟踪应该打印的三字符序列,但这并不是必需的,您可以直接在 (count == 3)
情况下打印.
import java.util.ArrayList;
public class PrintThreeOfFive
{
public static void main(String[] args)
{
char [][] one ={ {'a','b','c','d','e','f','g','h','3'},{'i','j','k','l','m','n','o','p','7'},{'q','r','s','t','u','v','w','x','2'}};
int aSize=5;
ArrayList<String> output = new ArrayList<String>();
char [] firstThree=new char[3];
for (int i=0; i< one.length;i++){
int count = 0;
for (int j=0; j < one[i].length; j++){
//re-set count at the 5th character
if (count == 4){
count = 0;
continue; //continue so that the fifth character is not added
}
//if in first three of five, add it to firstThree
if (count < 3){
firstThree[count]=one[i][j];
}
//increment count, and add to the list if at the third character
count++;
if (count == 3){
output.add(new String(firstThree));
}
}
}
for (String s : output){
System.out.println(s);
}
}
}
输出:
abc
fgh
ijk
nop
qrs
vwx
我认为@Masud 很接近,但他的解决方案有一些运行时间error.Change你的索引有点可以解决这个问题。
for(int m=0; m<3;m++){
firstThree[m]=one[i][m];
nextThree[m]=one[i][m+5];
}
我有一个二维字符数组,我想打印每个嵌套数组中每五个字符的前三个字符。这就是我正在做的事情:
char [][] one ={ {'a','b','c','d','e','f','g','h','3'},{'i','j','k','l','m','n','o','p','7'},{'q','r','s','t','u','v','w','x','2'}};
int aSize=5;
char [] firstThree=new char[3];
for (int i=0; i< one.length;i++){
for (int j=0; j< aSize;j++){
for(int m=0; m<3;m++){
firstThree[m]=one[i][m];
}
}
System.out.print(firstThree);
System.out.println("");
}
这给出了以下输出:
abc
ijk
qrs
我想要输出:
abc
fgh
ijk
nop
qrs
vwx
你可以这样做:
for (int j=5; j < 8;j++){
firstThree[j-5]=one[i][j];
}
您不需要 3 个循环来执行此操作...
您可以创建另一个数组以在 5 个索引之后读取,例如:
for(int m=0; m<3;m++){
firstThree[m]=one[i][m];
nextThree[m]=one[i+5][m];
}
这可以动态地进行,因此它可以用于更长的输入长度。
请注意,第三个嵌套循环不是必需的,您只需要完全遍历每个嵌套数组,使用 count
变量来跟踪添加前三个数组的位置每五个字符。
请注意,我还使用了一个 ArrayList<String>
来跟踪应该打印的三字符序列,但这并不是必需的,您可以直接在 (count == 3)
情况下打印.
import java.util.ArrayList;
public class PrintThreeOfFive
{
public static void main(String[] args)
{
char [][] one ={ {'a','b','c','d','e','f','g','h','3'},{'i','j','k','l','m','n','o','p','7'},{'q','r','s','t','u','v','w','x','2'}};
int aSize=5;
ArrayList<String> output = new ArrayList<String>();
char [] firstThree=new char[3];
for (int i=0; i< one.length;i++){
int count = 0;
for (int j=0; j < one[i].length; j++){
//re-set count at the 5th character
if (count == 4){
count = 0;
continue; //continue so that the fifth character is not added
}
//if in first three of five, add it to firstThree
if (count < 3){
firstThree[count]=one[i][j];
}
//increment count, and add to the list if at the third character
count++;
if (count == 3){
output.add(new String(firstThree));
}
}
}
for (String s : output){
System.out.println(s);
}
}
}
输出:
abc
fgh
ijk
nop
qrs
vwx
我认为@Masud 很接近,但他的解决方案有一些运行时间error.Change你的索引有点可以解决这个问题。
for(int m=0; m<3;m++){
firstThree[m]=one[i][m];
nextThree[m]=one[i][m+5];
}