Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do ArrayLists and Arrays interact with one another in Java?

How can we use Arrays and ArrayLists together (if possible at all)?

I want to store some Strings into an ArrayList, pull them out later, parse them up into String arrays, store those arrays in an ArrayList, and be able to retrieve the data from the ArrayLists later...

Check out this code, and feel free to take me apart for doing crappy coding; it's been a while since I used Java.

public static void parseData() throws IOException
{
    //for reference, nonParsedData is ArrayList<String>
    // while parsedData is ArrayList<String[]>
    String line = new String();
    String[] tempContainer = new String[7];
    Scanner keyboard = new Scanner(System.in);

    for (int x = 0; x < nonParsedData.size(); x++)
    {
        line = nonParsedData.get(x);

        //filling the temporary container to place into the ArrayList of arrays
        tempContainer[0] = line.substring(0,1); //Data piece 1
        tempContainer[1] = line.substring(1,3); //Data piece 2
        tempContainer[2] = line.substring(3,7); //Data piece 3
        tempContainer[3] = line.substring(7,8); //Data piece 4
        tempContainer[4] = line.substring(8,9); //Data piece 5
        tempContainer[5] = line.substring(9,10); //Data piece 6
        tempContainer[6] = line.substring(10,(line.length() - 1)); //Data piece 7



        parsedData.add(tempContainer);
    }
}

So in the previous, I've dumped some external file into nonParsedData. It's a bunch of strings. No big deal. I take those strings, read them, drop them into arrays. Hunky dory. That works fine.

Here is the retrieval process, which is making me apoplectic.

public static void fetchAndPrint() throws IOException
{
    String[] tempContainer = new String[7];

    for(int x = 0; x < parsedData.size(); x++)
    {

        tempContainer = parsedData.get(x); //this should be assigning my stored array to
                                           //the tempContainer array (I think)

        //let's try deepToString (that didn't work)
        //System.out.println((parsedData.get(x)).toString()); //just a debugging check

        System.out.println(x);
        System.out.println(tempContainer[0] + " " + tempContainer[1] + " " 
            + tempContainer[2] + " " + tempContainer[3] + " " 
            + tempContainer[4] + " " + tempContainer[5] + " " 
            + tempContainer[6] + " ");
    }
}

After I do this, only one of my outputs shows up from the initial ArrayList<String[]> - three times! Am I missing something in my loop? Am I implementing arrays from an ArrayList when there is really no functionality for this? Am I improperly assigning values to an array? Will ArrayList even give me back an actual array - or is it just a list like {1, 2, 3, etc.}? Are these things even different?

Finally, I know this can be implemented using a databasing language, but let's assume I want to stick with Java.

like image 351
Blake Burgess Avatar asked Aug 05 '26 18:08

Blake Burgess


1 Answers

I'll just keep it simple and answer your question:

How do arraylists and arrays interact with one another in java?

First we have to make distinction between the two.

While arrays (ie. tempContainer[]) are just a collection of like-typed data (primitives or objects) within contiguous memory, ArrayLists are objects with fields and member functions.

Back to your question. In order to convert from an array to an ArrayList:

String[] names = {"Bobby", "Earl", "Super Mario"};
ArrayList<String> namesList = new ArrayList<String>();

// First we must return the array as a List using Arrays.asList()
// Then use the addAll() method provided by the ArrayList class.
namesList.addAll(Arrays.asList(names));

Now for the opposite, returning the contents of an ArrayList into an array:

String[] names;
ArrayList<String> namesList = new ArrayList<String>();

//All that is needed is the ArrayList member method toArray().
names = namesList.toArray();

I hope this helps!

like image 112
Rafael Collado Avatar answered Aug 08 '26 11:08

Rafael Collado