How do ArrayLists and Arrays interact with each other in Java?

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

I want to store some strings in an ArrayList, pull them out later, parse them into String arrays, store those arrays in an ArrayList, and later get data from the ArrayLists ...

Have a look at this code and feel free to share me to do crappy coding; it has been a while since I was using 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 one I dropped some external file into nonParsedData

. It's a bunch of strings. Nothing wrong. I take these lines, read them, throw them into arrays. Hunky dory. This works great.

Here is the extraction process that makes 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 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 that, only one of my exits appears from the initial one ArrayList<String[]>

- three times! Am I missing something in my loop? Am I implementing arrays from ArrayList

when there is no functionality for that? Am I assigning values โ€‹โ€‹to an array incorrectly? Would it ArrayList

even return me a real 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 database language, but suppose I want to stick with Java.

+3


source to share


2 answers


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

How do arrays and arrays interact with each other in java?

We must first distinguish between the two.

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

Let's get back to your question. To convert from arrayin 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 the ArrayList to an array:

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

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

      

Hope this helps!

+1


source


Due to the scope, tempContainer

you are overwriting your values:

String[] tempContainer = new String[7];
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);
    // ...
    parsedData.add(tempContainer); // <=== this is always the same array
}

      

What you probably want is a new array for each iteration, for example:

for (int x = 0; x < nonParsedData.size(); x++)
{ 
   String[] tempContainer = new String[7];
   // fill tempContainer
   parsedData.add(tempContainer);

      



working demo


As a side note, you can use the object here:

  • Define an object for parts (let's call it Part

    )
  • Create a new object for each loop iteration
  • Use ArrayList<Part>

    forparsedData

0


source







All Articles