我想输出一个数组,比如int[] a={1,2,3,4,5}。我想达到的输出效果是1 2 3 4 5。但我现在只能是一个对话框一个对话框的输出,变成第一个对话框输出1,第二个对话框输出2....。看起来很不直观,所以请教大虾有没有办法将数组a的所有元素都显示在一个对话框里面。不甚感激!!!
{code}
import javax.swing.JOptionPane;
 
public class TestOutput {
 
    public static void main(String[] args){
     int[] a={1,2,3,4,5};
     for (int i = 0; i < a.length; i++)
// System.out.print(a[i] + " ");
JOptionPane.showMessageDialog(null, "The sort of this Array is:"
+ a[i], "Answer", JOptionPane.INFORMATION_MESSAGE);
    }
    
}{code}

解决方案 »

  1.   

    程序逻辑问题啊,你要只显示一个就要把所有的元素都存到一个变量里面,输出一次不就行了import javax.swing.JOptionPane; 
      
    public class TestOutput { 
      
        public static void main(String[] args){ 
         int[] a={1,2,3,4,5}; 
         String out = new String();
         for (int i = 0; i  < a.length; i++)
        {
     
              if (i == 0) {
                  out = String.valueOf(a[i]);
              } else {
                  out = out + "," + a[i];
              }
         }  
    JOptionPane.showMessageDialog(null, "The sort of this Array is:" 
    + out, "Answer", JOptionPane.INFORMATION_MESSAGE); 

      

  2.   

    import javax.swing.JOptionPane; 
      
    public class TestOutput { 
      
        public static void main(String[] args){ 
         int[] a={1,2,3,4,5}; 
         StringBuffer out = new StringBuffer();
         for (int i = 0; i  < a.length; i++)
        {
     
              if (i == 0) {
                  out = String.valueOf(a[i]);
              } else {
                  out = out + "," + a[i];
              }
         }  
    JOptionPane.showMessageDialog(null, "The sort of this Array is:" 
    + out.toString(), "Answer", JOptionPane.INFORMATION_MESSAGE); 
      

  3.   

    上面写错了,1楼的那个存在效率问题。建议用这个:
    import javax.swing.JOptionPane; 
      
    public class TestOutput { 
      
        public static void main(String[] args){ 
         int[] a={1,2,3,4,5}; 
         StringBuffer out = new StringBuffer();
         for (int i = 0; i   < a.length; i++)
        {
         out.append(a[i]);
             
         }  
    JOptionPane.showMessageDialog(null, "The sort of this Array is:" 
    + out.toString(), "Answer", JOptionPane.INFORMATION_MESSAGE);