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.