Skip to Main Content

New to Java

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Converting a static array to a dynamic list

843785Sep 1 2008 — edited Sep 1 2008
This is a followup to a separate post, but this is more inline with what I need.

Here is a working class, but as you can see I had to cheat and make a static 20 entry array. I much rather convert this into some sort of a dynamic ArrayList or other Java type (even a external class perhaps?)

XYDataset, TimeSeries, TimeSeriesCollection and Minute are all classes from jFreeChart.
convertDateTime is a local private class that I wrote to convert the textual csv entry into jFreeChart Minute Class.

Any help would be greatly appreciated.
private static XYDataset readData(String filename)
	{
		boolean headerAsLabel = true;
		TimeSeriesCollection data = new TimeSeriesCollection();

		try
		{
			CSVReader reader = new CSVReader(new FileReader(filename), ',');

			TimeSeries[] series = new TimeSeries[20];
			int actualSeriesLength = 0;
			String[] nextline = null;

			while ((nextline = reader.readNext()) != null)
			{
				String time;

				// There must be at least 1 data point to continue, this has a side effect of dropping out the HTML
				// headers in the file
				if (nextline.length > 3)
				{
					// ignore empty lines
					if ((!regexMatch("^$", nextline[0])))
					{
						actualSeriesLength = nextline.length - 2;
						if (actualSeriesLength > 20)
						{
							throw new RuntimeException("This program can not handle more than 20 data points.  This CSV contains " + actualSeriesLength + " data points.");
						}
						//
						// Learn the headers and create them as new TimeSeries
						//
						if (headerAsLabel)
						{
							for (int i = 2; i < nextline.length; i++)
							{
								series[i - 2] = new TimeSeries(nextline, Minute.class);
}
headerAsLabel = false;
}
else
{
// nextline header format: dateTime,label_ignore,header1,header2,...,headerN
time = nextline[0];
Minute relativeTime = null;
relativeTime = convertDateTime(time);
for (int i = 2; i < nextline.length; i++)
{
Integer v = Integer.parseInt(nextline[i]);
(series[i - 2]).add(relativeTime, v);
}
}
}
}
}
//
// Add the series arrays back into a single data variable to return
//
for (int i = 0; i < actualSeriesLength; i++)
{
System.out.println("i = " + i + " length " + series.length);
data.addSeries(series[i]);
}
}
catch (FileNotFoundException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
catch (ParseException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

return data;
}
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Sep 29 2008
Added on Sep 1 2008
6 comments
1,582 views