Using for loop to get all elements in an ArrayList? - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Using for loop to get all elements in an ArrayList?

Hi,

I'm trying to write a method that will use a for loop to iterate through all the positions in an ArrayList and add them to a String. I then want to return that String outside the for loop. Whenever I do this my for loop is ignored and what I initialised the String with is what is returned instead :S

[code]
String wordPos = null;
for (int i = 0; i < position.size() - 1; i++)
{
wordPos = wordPos + position.get(i);
}

return wordPos;
[/code]

I hope you can tell me what is going wrong or a better way of doing this.

Thanks


Comments

  • LozTaylorLozTaylor Posts: 3Member
    Hey there.

    First thing, you shouldn't initialise the String to a null. Instead of creating a blank String; which is what you want, it will create a String pointer pointing to a null object. If you do something like:

    [code]String a = null;
    Sysout.out.println(a);[/code]

    You should see it output 'null'. Instead, you should do:

    [code]String a = "";[/code]

    The second problem is that your loop won't cover all of the elements in the ArrayList. You don't need the:

    [code] i < position.size() - 1[/code]

    The '-1' is unnecessary here. If the size of your ArrayList is 10, you want to go through all of the elements from 0 to 9.

    Below is a working solution. I'm also making use of generics (the things in the '<>', as it's good practice, but you don't need to use them for this code to work).

    [code]
    import java.util.ArrayList;

    public class MessClass {

    public static void main (String[] args)
    {
    ArrayList myList = new ArrayList();

    myList.add("First");
    myList.add("Second");
    myList.add("Third");
    myList.add("Fourth");

    MyClass mc = new MyClass();

    System.out.println(mc.concatArrayList(myList) );
    }

    }


    class MyClass
    {

    public String concatArrayList(ArrayList list)
    {
    String wordPos = "";

    for (int i = 0; i < list.size(); i++)
    {
    wordPos = wordPos + list.get(i);
    }

    return wordPos;

    }

    }[/code]

    Hope this helps!

  • LozTaylorLozTaylor Posts: 3Member
    Hey there.

    First thing, you shouldn't initialise the String to a null. Instead of creating a blank String; which is what you want, it will create a String pointer pointing to a null object. If you do something like:

    [code]String a = null;
    Sysout.out.println(a);[/code]

    You should see it output 'null'. Instead, you should do:

    [code]String a = "";[/code]

    The second problem is that your loop won't cover all of the elements in the ArrayList. You don't need the:

    [code] i < position.size() - 1[/code]

    The '-1' is unnecessary here. If the size of your ArrayList is 10, you want to go through all of the elements from 0 to 9.

    Below is a working solution. I'm also making use of generics (the things in the '<>', as it's good practice, but you don't need to use them for this code to work).

    [code]
    import java.util.ArrayList;

    public class MessClass {

    public static void main (String[] args)
    {
    ArrayList myList = new ArrayList();

    myList.add("First");
    myList.add("Second");
    myList.add("Third");
    myList.add("Fourth");

    MyClass mc = new MyClass();

    System.out.println(mc.concatArrayList(myList) );
    }

    }


    class MyClass
    {

    public String concatArrayList(ArrayList list)
    {
    String wordPos = "";

    for (int i = 0; i < list.size(); i++)
    {
    wordPos = wordPos + list.get(i);
    }

    return wordPos;

    }

    }[/code]

    Hope this helps!

  • LozTaylorLozTaylor Posts: 3Member
    Hey there.

    First thing, you shouldn't initialise the String to a null. Instead of creating a blank String; which is what you want, it will create a String pointer pointing to a null object. If you do something like:

    [code]String a = null;
    Sysout.out.println(a);[/code]

    You should see it output 'null'. Instead, you should do:

    [code]String a = "";[/code]

    The second problem is that your loop won't cover all of the elements in the ArrayList. You don't need the:

    [code] i < position.size() - 1[/code]

    The '-1' is unnecessary here. If the size of your ArrayList is 10, you want to go through all of the elements from 0 to 9.

    Below is a working solution. I'm also making use of generics (the things in the '<>', as it's good practice, but you don't need to use them for this code to work).

    [code]
    import java.util.ArrayList;

    public class MessClass {

    public static void main (String[] args)
    {
    ArrayList myList = new ArrayList();

    myList.add("First");
    myList.add("Second");
    myList.add("Third");
    myList.add("Fourth");

    MyClass mc = new MyClass();

    System.out.println(mc.concatArrayList(myList) );
    }

    }


    class MyClass
    {

    public String concatArrayList(ArrayList list)
    {
    String wordPos = "";

    for (int i = 0; i < list.size(); i++)
    {
    wordPos = wordPos + list.get(i);
    }

    return wordPos;

    }

    }[/code]

    Hope this helps!

Sign In or Register to comment.