Problem parsing data and create new collection

Hi all,

I have a programming problem, which have difficulty to find a good solution for. This is the info:

Goal:

--

Given an input file in plain text, each line specifying a "path", build an in-memory

ontology (directed graph) that can be queried against.

Specification:

--

Input:

Raw text file, each line being a "path" or "class" in the ontology.

The lines are of the format:

RootName|PathElementName|PathElementName|....|LeafName

For example:

Film|Tapes|Media

Represents one concept of 'Media', namely that 'Media' is a type of 'Tapes', which is a type of 'Film'.

Names that contain commas or ampersands should be split up.

For example:

Film,Movies|Tapes|Media,Film

Will become the four paths in the ontology:

Film|Tapes|Media

Film|Tapes|Film

Movies|Tapes|Media

Movies|Tapes|Film

My solution: (I don抰 paste the actually lines of program here):

I parsed the input based on 搢?which gives me an String[] fields:

For this ex.: Film,Movies|Tapes|Media,Film

fields[] = {[Film,Movies], [Tapes], [Media,Film]}

For each member if the fields[], we need to parse the element based on ',', which gives us a new String[]. So for

the above ex. with three members, we will have 3 new String[]. We can keep each in an ArrayList, which means we will have an ArrayList when the members are array. Then we can operate on each array.

*/

List parsedFields =new ArrayList();

for (int a = 0; a < fields.lenght; a++)

{

String patternStr =",";

String[] commaFields = fileds[a].split(patternStr);

ParsedFields.add(commaFields);

}

/*At this point I have an ArrayList: parsedFileds which has as many members as our original array: fields[]. Each member is an array by itself.

parsedFileds --> [{film, movies}, {tapes}, {media, film}]

I tried to go thru each array in turn and create ArrayList like:

[film, tapes, media]

[film, tapes, film]

[movies, tapes, media]

[movies, tapes, film]

I have difficulty to find a good solution to create the above arraylists, I needed to create many loops, ?and I find it very confusing. Does anybody can think of any solution that can help me accomplish this task? Any help is greatly appreciated.

[2573 byte] By [RonitTa] at [2007-11-27 7:05:35]
# 1

Ok, so with the following line:A,B | C,D,E | F,G,H,I

then you'd have 2*3*4 = 24 walks.

You could generate a 2D int array of 24*3 which would look like this:

24*3 matrix walk

0 0 0A,C,F

0 0 1A,C,G

0 0 2A,C,H

0 0 3A,C,I

0 1 0A,D,F

0 1 1

0 1 2

0 1 3

0 2 0

0 2 1

0 2 2

0 2 3

1 0 0...

1 0 1

1 0 2

1 0 3

1 1 0

1 1 1

1 1 2

1 1 3

1 2 0

1 2 1

1 2 2B,E,H

1 2 3B,E,I

You see the pattern? The last column just repeats indexes 0,1,2,3 a number of times. The middle column repeats 0,1,2 but then with a repetition of 4 (the repetition size of the column to the right). And lastly, the fist column: it only displays two different indexes: 0 and 1 but then with a repetition of 12 (which is the size of the columns to it's right multiplied to each other).

Now try to construct your algorithm from the above.

Good luck.

prometheuzza at 2007-7-12 18:56:49 > top of Java-index,Java Essentials,Java Programming...