如何打印这个数组?

时间:2013-01-04 11:57:47

标签: java

  

可能重复:
  Simplest way to print an array in Java

public char[] createArray() {        
  char[] s;        
  s = new char[26];

  for ( int i=0; i<26; i++ ) {    
    s[i] = (char) (’A’ + i);   
  }            
  return s;

  // how to print s?

  }

2 个答案:

答案 0 :(得分:5)

如果要打印为数组

System.out.println(Arrays.toString(s));

如果要打印为字符串

System.out.println(new String(s));
是的,你可以写

char[] s = new char['Z' - 'A' + 1];
for(char ch = 'A'; ch <= 'Z'; ch++)
   s[ch - 'A'] = ch;

答案 1 :(得分:2)

您可以使用Arrays.toString(s)并打印生成的String