How to but string values into String matrix. How to write the for loop?

I want to create a String matrix (50 by 2) and fill it with string information. Im am collecting the string values by iterating through a HTMLdocument. I don't know how to write the for loop when dealing with matrices. I want to put the links ("link" on the code) in one column and then the linkinfo ("text" in the code) in the other one.

String[][] data =new String[50][2];

publicvoid getLinks(){

HTMLDocument.Iterator iterator = htmlDoc.getIterator(HTML.Tag.A);

while (iterator.isValid()){

String link = (String)iterator.getAttributes().getAttribute(HTML.Attribute.HREF);

System.out.println(link);

//Some kind of for loop to but the the String "link" into place...?

try{

//Some kind of for loop to but the the String "text" into place...?

String text = htmlDoc.getText(iterator.getStartOffset(), iterator.getEndOffset()-iterator.getStartOffset());

System.out.println(text);

}

catch (Exception e){

}

iterator.next();

}

}

Any tip?

[1520 byte] By [Siggefa] at [2007-11-27 4:06:31]
# 1

You would be better off using a List of objects of a class that you create. For example:

class Link {

public String href;

public String text;

}

At the beginning of the code you would create a new List<Link>, and then each time you encounter an <A> tag, create a new Link object with the href and text, and add it to your list. (The creationof the Link object would go where the System.out.println(text); currently is.)

Why is this better than the 50x2 String matrix?

1. you don't need to define an upper limit to the number of links (is 50 always going to be enough?)

2. you won't be left with empty elements in the array when the code has finished (the List<Link> would just contain the right number of items)

3. the object-oriented approach (i.e. using a new class instead of a 2-String array) is more meaningful: having "a list of links" is easier to understand than having a 2D String array. It makes iterating through the links, and retrieving each link's properties, much more readable.

RichFearna at 2007-7-12 9:11:39 > top of Java-index,Java Essentials,New To Java...