始终remeber的数组索引从0
[E][x][a][m][p][l][e]
0 1 2 3 4 5 6
开始我不明白你为什么用字符数组打破你的头,当你有字符串。 在这里,我提出你一个解决方案,其中:
1变换炭[]为String
2-迭代字符串对象,并使用从String类的一些方法,以获得所需的结果:
public class Example
{
public static void main(String[] args) {
//Here an array with 28 characters
char[] array = {'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','!','@'};
//For easier manipulation i will just transform that array to String
//Why not, String was invented so we dont have to use char[].
String arrayAsString = new String(array);
//Use the methods that String class provides, to make the solution easier
for(int i=0;i < array.length;i++) {
System.out.println((i+1) + " " + arrayAsString.substring(0,(arrayAsString.length()-i)));
}
}
}
为什么索引从28开始? 'index <= nameArray.length'将产生一个错误。你为什么试图向后打印数组? – 2011-06-11 17:16:14
伙计@ SO ...这是这个家伙关闭2天前提出的同样的问题:http://stackoverflow.com/questions/6286524/using-an-if-statement-to-examine-an-array -in-java – 2011-06-11 17:32:09
这不是同一个问题。我有一个分配使用相同的数组与多个部分。我已经明白了这一点。 – JDB 2011-06-11 17:38:30