how do i use ArrayList with multiple collumns?
How do define and use the Java ArrayList so that i can have multiple items stored side by side? kinda hard to explain so here is a picture.
|Date__|__Time__|_Name__|
||__|__|
||__|__|
--
now i could create multiple arrays but then i run into the possibility of the results not matching.
my application is just a parser that takes the input and stores the data in an arraylist then i will recall the data in the proper format or exclude based on what is needed. i can get the application to work if i can understand how to use the arraylist in the way that i need. i have done this a while back with a std array but that has defined size. please please please someone help me.
[719 byte] By [
maramora] at [2007-11-27 10:16:28]

Create a class that holds a Date, Time, and Name. Then parse the input into objects of that class. Then add those objects to the List.
While you can have a list of lists, it makes more sense to define an object with properties Date, Time, Name etc..., call it say CallEntry, and have a List < CallEntry >.
Is a list of lists better than an arraylist? or am i just missing it. Does it work in the same way with going line by line interator? Do you use a list to define the arraylist? I am going to be storing large amounts of data, like 80,000 records from txt file, LoL, any problems.
I havent used a list in Java, ill look that up and see what i can do with that.
Message was edited by:
maramor
Whoa, slow down! Class ArrayList is one possible implementation of interface List. Java programmers prefer to talk about List rather than ArrayList, unless they are referring to specific aspects of ArrayList, like its constructors.
What are you doing with the data? You may not even need a List/Collection/Array at all. If you were say reading in the lines, changing them and then re-writing them to a new file then you don't need a container.
BigDaddyLoveHandles:
Ok ok I think I understand that now, LoL Sorry.
So you are suggesting, would a method be advised, separate class, or something else?
Create a ?method? that includes the columns (parameters) that are needed then when declaring the ArrayList use ArrayList<method name> myArray = new ArrayList< method name >();
If I got this wrong can you please supply a example.
_helloWorld_:
At first I was going to store the whole txt in an array then perform the parsing but that sounds like a lot of overhead even for learning.
I am storing the data in a txt file, going to pull it into an arraylist parsing each needed line excluding lines that dont fit my parse delimiters (multiple delimiters as there are two repeated lines that store the data needed with other non-needed lines all over the place).Then once the parsing is done I will write arraylist to file in the format needed adding any other appending data if needed.
I am sure I could bypass the arraylist by reading one line at a time, processing, formatting then writing to file which does hold its value in speed and I will probably work on doing this next. I am still learning java and learning new things is what you do when you are new. I always start complex and then work my way to improve, learning along the way so I dont do it when I need something on a time schedule, LoL.
My test data is simply a dos dir dump parsing the modify Date and Time, root-directory, folders, optional modifier(name) (no files).
My one line test, as I dont have file controls working yet (I had to modify the L:\CSD to L:\\CSD, hopefully this will not be the case when reading the file.)
DosDirParser myTest = new DosDirParser(" Directory of L:\\CSD", "Directory of");
DosDirParser myTest1 = new DosDirParser("04/05/2005 02:42 PM<DIR> Accessories, updates","<DIR>");
My tokenize method:
private void tokenize(String input, String delimiter)
{
StringTokenizer st = new StringTokenizer(Input, Delimiter);
int sttest = st.countTokens();
int sttest1 = 1;
if (this.Delimiter.equalsIgnoreCase("<DIR>"))
{
if (sttest1 <= sttest)
{
this.setModifyDAndT(st.nextToken().trim());
sttest1 = sttest1 + 1;
}
if (sttest1 <= sttest)
{
this.setDirectory(st.nextToken().trim());
sttest1 = sttest1 + 1;
}
}
else
if (this.Delimiter.equalsIgnoreCase("Directory of"))
{
if (sttest1 <= sttest)
{
this.setRootPath(st.nextToken().trim());
sttest1 = sttest1 + 1;
}
}
}
import java.util.*;
public class Person {
private String name;
private int shoeSize;
public Person() {}
public Person(String name, int shoeSize) {
setName(name);
setShoeSize(shoeSize);
}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public int getShoeSize() {return shoeSize;}
public void setShoeSize(int shoeSize) {this.shoeSize = shoeSize;}
public String toString() {return "(" + getName() + "," + getShoeSize() + ")";}
//demo
public static void main(String[] args) {
List < Person > persons = new ArrayList < Person > ();
persons.add(new Person("BDLH", 14));
persons.add(new Person("Mary", 7));
persons.add(new Person("Jose", 10));
System.out.println(persons);
}
}
thinks a bunch, that is exactly what i wanted to see, makes since now.
Here is my code, all i get is [null, null, null]?
package directroyParser;
import java.util.ArrayList;
import java.util.List;
public class DosDirParserContainer {
private String Delimiter;
private String Input;
private String ModifyDAndT;
private String Directory;
private String RootPath;
public DosDirParserContainer()
{
}
public DosDirParserContainer(String delimiter, String Input, String ModifyDAndT, String Directory, String RootPath)
{
setDelimiter(delimiter);
setInput(Input);
setModifyDAndT(ModifyDAndT);
setDirectory(Directory);
setRootPath(RootPath);
}
public void setDelimiter(String delimiter) {
Delimiter=delimiter;
}
public String getDelimiter() {
return Delimiter;
}
public void setInput(String input) {
Input = input;
}
public String getInput() {
return Input;
}
public void setModifyDAndT(String modifyDAndT) {
ModifyDAndT = modifyDAndT;
}
public String getModifyDAndT() {
return ModifyDAndT;
}
public void setDirectory(String directory) {
Directory = directory;
}
public String getDirectory() {
return Directory;
}
public void setRootPath(String rootPath) {
RootPath = rootPath;
}
public String getRootPath() {
return RootPath;
}
public String toString()
{
if (this.Delimiter.equalsIgnoreCase("<DIR>"))
{
return this.getDirectory() + "\\\t" + this.getModifyDAndT();
}
else
if (this.Delimiter.equalsIgnoreCase("Directory of"))
{
return this.getRootPath() + "\\";
}
return null;
}
public static void main(String[] args) {
List <DosDirParserContainer> DosDirParserContainers = new ArrayList <DosDirParserContainer>();
DosDirParserContainers.add(new DosDirParserContainer("delimiter", "Input","ModifyDAndT","Directory","RootPath"));
DosDirParserContainers.add(new DosDirParserContainer("1", "2","3","4","5"));
DosDirParserContainers.add(new DosDirParserContainer("a", "b","c","d","e"));
System.out.println(DosDirParserContainers);
}
}
Message was edited by:
maramor
Message was edited by:
maramor
Try this for a toString method:
public String toString()
{
if (this.Delimiter.equalsIgnoreCase("<DIR>"))
{
return this.getDirectory() + "\\\t" + this.getModifyDAndT();
}
else
if (this.Delimiter.equalsIgnoreCase("Directory of"))
{
return this.getRootPath() + "\\";
}
return "my toString needs work";
}
LoL, good one, LoL ok ok so i over looked the fact that it was using the toString, LoL.
thanks again.
ok i can work on the toString, now how would i retrieve this data from the list and manuver the pieces around, for example:
if wanted to go through the list and add or change something. Since you are using the toString would't i have to reparce that string to seperate them?
or would it be something like
(looping with iterator?)
Person myPerson = new Person();
myPerson.persons.get[1]; (or i if loop)
System.out.println(myPerson.getName());
still investigating......
Message was edited by:
maramor
In simple cases, the enhanced for loop can take of iteration (it uses an implicit iterator):
import java.util.*;
public class Person {
private String name;
private int shoeSize;
public Person() {}
public Person(String name, int shoeSize) {
setName(name);
setShoeSize(shoeSize);
}
public String getName() {return name;}
public void setName(String name) {this.name = name;}
public int getShoeSize() {return shoeSize;}
public void setShoeSize(int shoeSize) {this.shoeSize = shoeSize;}
public String toString() {return "(" + getName() + "," + getShoeSize() + ")";}
//demo
public static void main(String[] args) {
List < Person > persons = new ArrayList < Person > ();
persons.add(new Person("BDLH", 14));
persons.add(new Person("Mary", 7));
persons.add(new Person("Jose", 10));
//NEW STUFF:
for(Person p : persons) {
String name = p.getName();
System.out.println("Hello, " + name + "!");
}
}
}
Sweet, that takes care of every thing, thanks for working with me and being patient.
I think I can work from here, I hope, LoL.