0

I have a Text File having following lines

1) 2,3 3,2 4,5

2) 1,3 4,2 6,13

3) 1,2 4,2 5,5

4) 1,5 2,2 3,2 5,4 7,3 6,6

The number of lines of the text file is known (i,e 4). The pair digits (for eg : 2,3) are variable for each line. Whereas the pair digit corresponds to the property.

So, to represent these values i am planning to have an array of linked list of an array.

The reason for this choice of mine is that:

1 : Since i know the number of lines is 4. So it will be an array.

2 : Each line has a variable number of pairs. So, i will have a linked list representation for it.

3 : The pair values each represents a property so i will have an array size two for it. One index for each property.

Considering the above three points, I will have an Array(size 4) of LinkedList of an Array(size 2).

Here is how i have represented this in Java(I am new to Java and i have to confess that i do not know yet if its correct ).

static ArrayList<Integer[]>[] graph = (ArrayList<Integer[]>[]) new ArrayList[200];

Now, considering my above graph variable has been initialized properly can some one tell me how can i populate it with the data. If you have some other advice regarding the initialization or how i have conceptually broken the problem in 3 points please share.

1
  • What's holding you back from populating it with data? Commented Jul 22, 2012 at 17:48

3 Answers 3

4

If I were you, I'd keep it simple. Just use something like

List<List<List<Integer>>>

or even better, create your own class for the inner types.

List<PairList>

or something like that, where PairList extends (or contains) an ArrayList<Pair>.

Sign up to request clarification or add additional context in comments.

Comments

1

Though your thinking process is comprehensible, I think you have come to incorrect decisions (maybe caused by your lack of Java experience) - here are the answers I suggest:

  1. You know that the number of lines is constant with a known constant, but using arrays in Java directly is mostly not useful, use ArrayList instead. It is a List (a tuple) implemented using an array.
  2. Your thought on modeling the pairs of variable amount as a list seems very good and I would agree to this. You should, however, not use a LinkedList for this purpose, as it is generally slower than an ArrayList. Both implementations satisfy the List interface so you can use them interchangeable.
  3. You could model the pairs themselves as two element arrays, but to make the semantics more understandable, you should invent a documented data class for the two parts.

Now, let's assume the container type would look like this (you should give the variables and the class itself better names):

class Pair {
  public final int a;
  public final int b;

  Pair(int a, int b){
    this.a = a;
    this.b = b;
  }
}

You could then create your data structure with:

final int lines = 42; // the number of lines
List<List<Pair>> data = new ArrayList<List<Pair>>(lines);

And fill it with data using

for(String line : input){
  final List<Pair> lineList = new ArrayList<Pair>();
  lineList.addAll(extractPairsFromLine(line));
  data.add(lineList);
}

where extractPairsFromLine is a method taking a String and returning a List<Pair>.

Comments

1

Use Collection framework.

ArrayList<ArrayList<ArrayList<Integer>>>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.