4 Replies Latest reply: Apr 3, 2009 5:38 PM by 843789 RSS

    How do i get a String out of an ArrayList?

    843789
      I was trying to make a list I could grow and I was informed that arrays couldn't grow, an ArrayList was suggested.

      This seems like it will work perfectly except for the fact that I can't retrieve a String from the ArrayList.

      I'm getting errors like incompatible type String:Object or expected:[] found ArrayList.

      I tried .toString but that just gives me a bunch of giberish.

      Any advice would be most welcome.

      Thanks in advance,

      Hans.
        • 1. Re: How do i get a String out of an ArrayList?
          doremifasollatido
          hansbig wrote:
          Any advice would be most welcome.
          Post code, using code tags (see CODE button above posting box). Cut and paste the EXACT error message into your post.
          • 2. Re: How do i get a String out of an ArrayList?
            843789
            Ok, so, i guess here is some code you can run, it should run.

            I guess I want to know how to get each individual string out of the list one at a time.
            package arraylistone;
            
            import javax.swing.JFrame;
            import java.util.ArrayList;
            import javax.swing.JLabel;
            
            public class Main extends JFrame
            {
                        JLabel label = new JLabel();
                        ArrayList list = new ArrayList();
            
                public Main()
                {
                    String str = "test";
                    String str2 = "test2";
                    list.add(str);
                    list.add(str2);
                    label.setText("Not Working");
                    label.setText(list.toArray().toString());
                            getContentPane().add( label);
            
                }
                public static void main(String[] args)
                {
                    Main frame = new Main();
                    frame.setDefaultCloseOperation( EXIT_ON_CLOSE );
                    frame.pack();
                    frame.setLocationRelativeTo( null );
                    frame.setVisible(true);
                }
            }
            • 3. Re: How do i get a String out of an ArrayList?
              843789
              import java.util.*;
              
              public class Test {
                  public static void main(String[] args) {
                      List<String> strings = new ArrayList<String>();
                      strings.add("alpha");
                      strings.add("beta");
                      strings.add("gamma");
              
                      System.out.println("accessing a string: " + strings.get(2));
                      System.out.println("all strings:");
                      for(String s : strings) {
                          System.out.println(s);
                      }
                  }
              }
              You should review new features of Java 5, like generics and the enhanced for loop: [http://java.sun.com/j2se/1.5.0/docs/relnotes/features.html#lang]
              • 4. Re: How do i get a String out of an ArrayList?
                843789
                thanks :)