4 Replies Latest reply: Jul 11, 2007 9:56 PM by 807605 RSS

    Converting an ArrayList Element into a String?

    807605
      Hi,

      I'm not sure if this is possible, but is there a way to convert an ArrayList Element into a String?
        • 1. Re: Converting an ArrayList Element into a String?
          807605
          Could you be a bit more vague, please.
          • 2. Re: Converting an ArrayList Element into a String?
            807605
            Have you tried toString() ?

            If you need a String with the contents of the List then run a loop and concatenate the contents of the List into the String

            ICE
            • 3. Re: Converting an ArrayList Element into a String?
              807605
              I'm trying to create code, where a class calls upon the reform method.
              When referencing this code, I want to use a string.
              //List Sorted by title:
                        System.out.println("List Sorted by title: ");
                        ArrayList s = new ArrayList();
                        CollectionUtil c = new CollectionUtil();
                        c.reform();
              -I want to add a string variable in c.reform(), where the reform method can modify the string.

              In the reform method, I want to use a string parameter,
              Here's my code:
              public void reform()
                   {
                        //format input
                        ArrayList<String> list = new ArrayList<String>();
                              // a test
                        list.add("This");
                        list.add("is");
                        list.add("is");
                        list.add("a");
                        list.add("a");
                        list.add("test");
                        displayList(list);
                   }
              
                   public void displayList(ArrayList<String> list) 
                   {
                        System.out.println("The size is: " + list.size());
                        for (int i = 0; i < list.size(); i++) 
                        {
                             String title = list.get(i);
                             System.out.println(title); 
                        }
                   }
              • 4. Re: Converting an ArrayList Element into a String?
                807605
                Still no idea what you want. But since your list already holds Strings then there is no need to do any converting. Just get an element back out.