简体   繁体   中英

Displaying an array in a TextView in java

hi i made a program that counts the elements in an array and i done it already. Now i want to display the result in a textView.. I want to display it this way...

1 appeared 2times
2 appeared 1times
3 appeared 1times
6 appeared 1times

this is my code..

The last element only displays in the textView..
please help me..Thanks

 String []values = ( input.getText().toString().split(","));
 Arrays.sort(values);
 int c=1,i=0,range=4;
 while(i<values.length-1){
     while(values[i]==values[i+1]){
         c++; 
         i++;   
     }   
     jLabel7.setText(values[i] + " appeared " + c + " times");            
     c=1;
     i++;
     if(i==values.length-1)
         jLabel7.setText(values[i] + " appeared " + c + " times");
 } 

Try this:

jLabel7.setText(jLabel7.getText() + "\\n" + values[i] + " appeared " + c + " times");

Att: If u are using swing componentes, is not a TextView but JLabel, or u are working for Android?

update your code to:

 String []values = ( input.getText().toString().split(","));
 Arrays.sort(values);
 int c=1,i=0,range=4;
 while(i<values.length-1){
     while(values[i]==values[i+1]){
         c++; 
         i++;   
     }   
     jLabel7.setText(jLabel7.getText() + "\n" + values[i] + " appeared " + c + " times");          
     c=1;
     i++;
     if(i==values.length-1)
         jLabel7.setText(jLabel7.getText() + "\n" + values[i] + " appeared " + c + " times");
 }

将所有值附加到单个String对象中,然后使用setText方法显示字符串

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM