0

I am quite new to java currently working on a not-so-simple web browser application in which I would like to record a permanent history file with a 2D array setup with 3 columns containing "Date Viewed", "URL", "How many times this URL has been viewed before".

Currently I have a temporary solution that only saves "URL" which is also used for "Back, Foward" features using an ArrayList.

private List tempHistory = new ArrayList();

I am reading through the Java documentation but I cannot put together a solution, unless I am missing the obvious there is no 2D array as flexible a ArrayList like in Python?

Kai
  • 38,985
  • 14
  • 88
  • 103
Test_Subject
  • 59
  • 1
  • 2
  • 8

3 Answers3

0

From your description it doesn't sound like you need a 2D array. You just have one dimension -- but of complex data types, right?

So define a HistoryItem class or something with a Date property for date viewed, URL for URL, int for view count.

Then you just want a List<HistoryItem> history = new ArrayList<HistoryItem>().

The reason I don't think you really want a 2D array-like thing is that it could only hold one data type, and you clearly have several data types at work here, like a date and a count. But if you really want a table-like abstraction, try Guava's Table.

Sean Owen
  • 66,182
  • 23
  • 141
  • 173
  • Thank You, I think your approach sounds most suited, but to clarify my understanding, I simple set up a class to store there three variables, designate them to a single line and save to a binary file instead of requiring an array? My concern was maintaining the data types to easily sort after they have been loaded. I will give this a try and report result, thank you. – Test_Subject Apr 07 '13 at 19:27
  • You mention several different converns. If you want to save these objects to a binary file, look into `Serializable`. If you want them to be sortable, look into implementing `Comparable` or make a `Comparator`. – Sean Owen Apr 07 '13 at 21:07
0

No, there is no built-in 2D array type in Java (unless you use primitive arrays).

You could just use a list of lists (List<List>) - however, I think it is almost always better to use a custom type that you put into the list. In your case, you'd create a class HistoryEntry (with fields for "Date viewed", URL etc.), and use List<HistoryEntry>. That way, you get all the benefits a proper type gives you (typechecking, completion in an IDE, ability to put methods into the class etc.).

sleske
  • 81,358
  • 34
  • 189
  • 227
0

How do you plan to browse the history then? If you want to search the history for each url later on then ArrayList approach might not be efficient.

I would rather prefer a Map with URL as key.

Map<Url,UrlHistory> browseHistory = new HahMap<Url,UrlHistory> ();

UrlHistory will contains all the fields you want to associate with a url like no. of times page was accessed and all.

Lokesh
  • 7,810
  • 6
  • 48
  • 78