Link to home
Start Free TrialLog in
Avatar of Vel Eous
Vel Eous

asked on

arrayList toString

I have an arrayList which is populated from a text file.

I then change certain arrayList index elements .remove() and .add(i, newString).  At the end of that I am still left with an arrayList.

What I am looking to do is turn that arrayList back into a string so it can be displayed on screen in a JTextArea before being saved back to a text file.

I did try:

String str = arrayList.toString();
jtextAreaOutput.setText(str);

But that produced:

[element1, element2, etc]

How can this be resolved ?

Thanks,
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

You need to have some way of joining them. You could do

ta.setText(arrayList.toString().replaceAll(",", " ");

You want to remove the '[' and ']'
It all depends on what format you wish to display the contained elements. Seems like you are  not satisfied with the default implementation of  ArrayList. All you have to do is iterate over the list and format the retrieved elements.
For eg, if you need to display the strings as CSV
String csv = "";
for(int i=0; i < list.size();i++){
   csv += (String)list.get(i)+",";
}
if(csv.length() > 1){
 csv += csv.substring(0, csv.length());
}
try overloading the ArrayList's toString() method.

if you have edited remove(), add(), etc, try to look for a toString()

If none, then simply write one in the class.

Avatar of Vel Eous
Vel Eous

ASKER

OK, I attemped what you said:

public void find() {
            
            String find = Jaypad.searchString;
            String tempString = null;
            int x = theFile.size();
            
            for (int i=0; i<(x); i++) {
                  if (theFile.get(i).equals(find)) {
                        theFile.remove(i);
                        theFile.add(i, "REPLACED");
                        tempString = theFile.toString().replaceAll(",", "");
                        System.out.println(tempString);
                        
                  } else {
                        Jaypad.jlblStatus.setText("Not found");
                  }
            }
            Jaypad.jtextAreaDisplay.setText(tempString);
            Jaypad.jlblStatus.setText(find);
      }

^^ works fine, removes the "," from between array elements.  When I attempt to replace all "[" or "]" symbols though, I get a runtime error.
>> When I attempt to replace all "[" or "]" symbols though, I get a runtime error.

It's just a simple substring operation. Can you post the code you tried?
Its the code above but replacing :

tempString = theFile.toString().replaceAll(",", "");

with:

tempString = theFile.toString().replaceAll("[", "");
String newText = new StringBuilder(arrayList.toString().replaceAll(",", " ")).deleteCharAt(newText.length() - 1).deleteCharAt(0).toString();
StringBuffer sb = new StringBuffer() ;
for( String s : arrayList )
  sb.append( s ) ;

I'm probably missing the point though... ;-)
ASKER CERTIFIED SOLUTION
Avatar of TimYates
TimYates
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
What was the exception you got and could you print its stack trace?
I went about it a different way in the end.  Used a string buffer.
You can also try StringBuilder if you are using Java 5.0 or 6.0
StringBuilder is to be preferred over StringBuffer if you can use it
>> I went about it a different way in the end.  Used a string buffer.

My example used a StringBuffer...  Did you go that way then?
Looks like ;)
Sorry, not feeling well and I`m not taking things in very well at the moment.  I've ended up with the following which outputs the arrayList minus all that array stuff ( [ , ] etc ):


// search & replace all method
      public void findAndReplaceAll() {
            
            String find = Jaypad.searchString;
            int x = theFile.size();
            
            Object obj = null;
            StringBuilder strBuild = new StringBuilder();
            
            for (int i=0; i<x; i++) {
                  if (theFile.get(i).equalsIgnoreCase(find)) {
                        theFile.set(i, "REPLACED");
                        System.out.println(theFile);
                        obj = theFile.get(i);
                        strBuild.append(obj + " ");
                  } else {
                        obj = theFile.get(i);
                        strBuild.append(obj + " ");
                  }
            }
            String result = strBuild.toString();
            Jaypad.jtextAreaDisplay.setText(result);
      }

Thing is my array appears to be skipping over elements that match my search criteria.  :/
>> theFile.set(i, "REPLACED");

Should this not be done after: >> obj = theFile.get(i);
Why though ?

I want to replace the element, then send the new element to the StringBuilder.  If put obj = theFile.get(i); before I replace the element I`m just sending the old element to the StringBuilder am I not ?
I thought you wanted to append the old ones, sorry. But in your code, the new element is always the String "REPLACED" right? 'theFile' is the ArrayList?
yes.
Not sure I deserved all the points here...
Well I went with your solution ... ?  :P
What is the output you expect and what is the output you get?
It's cool, I resolved the issue using the StringBuilder method.

arrayList.toString().replace('[',' ').replace(']',' ').replaceAll(" ","")

Open in new window