• Post Reply Bookmark Topic Watch Topic
  • New Topic

Object in list holds a reference to another object in the same list  RSS feed

 
Martin Traedholm
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

I have some data from a XML file which holds calendar information. The data I get from the XML file is when event arrives and notification to a certain event arrives. Those data needs to be sorted in the manner of the element arrives first is at the top. Furthermore the notification needs to know the event it is refering t, in others words the notification needs to know the index number of the event in the list.

Im fairly new in Java but learning, anyway here is my code for a solution to the problem describe above and would like to know if there is something that could go wrong (especilly with the way I have implementet the way of notification knowing the index of event)








This gives the following output:
2012 06 21 12 00
test.Martin.Event@94884d
Pointer to Event in list: 1
2012 06 21 12 30
test.Martin.Event@94884d
2012 06 21 13 00
test.Martin.Event@1807ca8
Pointer to Event in list: 3
2012 06 21 13 30
test.Martin.Event@1807ca8


Edit: edit headline
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is worth considering recording the system time instead of a Calendar object. You can record that as a long. You can make that field final, so it never changes. Then you can’t use a setTime() method, and I think the getTime method does not need to be synchronized. (Somebody please tell me if I am mistaken). You can display the time with the %t tags, which you can find details of in the Formatter documentation, or you can set the time in a Calendar object with its setTimeInMillis(long) method.
I suggest you never permit the time to be 0, so put something like this in the constructorYou can of course do the same if you use an ObjectMake all your instance fields private, and maybe final as well. Don’t give fields public access unless they are to be used as constants.
What do the three byte fields mean? Why are they in that class in the first place?
Look for chapter 3 of Joshua Bloch’s Effective Java, which tells you about the toString() method. You will need to override toString().

In the Notification class, get rid of the assignment to null, which is unnecessary. I suggest another test in the constructor that the Event object is not null, as above. It should have private access, again, and if access is required, consider a get method. The name pointerToObject appears inappropriate; choose a more descriptive name.

And welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you are getting the calendar information from an XML file, you can’t use the milliseconds, but you can get milliseconds from a Calendar object thus. I am a little worried about having the fields public, because they can be changed from outside your class. Similarly you can change a Calendar object’s state outside the class, but if you return a private long, other code cannot change the value inside the class.
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sould the notification class extend the MetaWatchDataV2 class? What about this?But why are you putting what appears to be a subclass instance inside the Notification class? Are you trying to create a linked list of Notifications or something?
 
Martin Traedholm
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Sould the notification class extend the MetaWatchDataV2 class? What about this?But why are you putting what appears to be a subclass instance inside the Notification class? Are you trying to create a linked list of Notifications or something?


Hey thanks for the reply. I put in an Object Event in the class because I need to know which object in the list (see below) is related to that notification, and I wanted to hear if there is any pitfalls doing that. After the list is sorted I need to know witch index in the list each notification point to.

 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not quite sure about that. I am not convinced that a List is the most appropriate collection, nor about your class design. You appear to have two classes whose instance appear to cause each other to occur. In which case, maybe a better name for pointerToObject might be cause.
Why do you need to sort the List? Do your objects form a series?
Can you draw the objects in diagrammatic form? That might give you a better idea of what sort of collection to use.
 
Martin Traedholm
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey
I don't know how to make UML or simular well enough. But I made a quick sketch of the system

The sorting is according to time, the lowest one first.

From an android unit I read an XML file, unpack the calendar information and add event/notification to single a list. The reason why I put into a single list is mainly because I shall know which index of an Event a given notification point at. The reason I need that information is because I need to send all that information to a Digital watch (MetaWatch) and that just see a long sorted array with both events and notification in it.

Hope it makes sence.

Edit: I need to learn how to spell
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So you want the information out of the XML file, ordered according to the time associated with it? That is obviously what the compareTo() method was for.
So you need a class which encapsulates those data, as well as the time. Or the date. I am not convinced that Coparable is quite right for that class, because you can’t say it has a natural ordering, but you can sort with the similar Comparator. Beware: if you use milliseconds, simply subtracting the two values can give the wrong answer because the range of a long is much larger than an int. In fact, if you use ints for milliseconds, you exhaust the entire range in about 3½ weeks.

If your class has a getMilliseconds() methdo, you can probably implement a Comparator like thisThe ?: operator associates to the right, so the last line is like this:
return time1 > time2 ? 1 : (time1 == time2 ? 0 : -1);
It means if the two are the same, 0, otherwise -1. But the -1 can be overruled by the outer ?: which returns 1 if time1 > time2.
You can use the compareTo method in two Date or Calendar objects instead. You can sort in ascending or descending order by moving the - sign. There is more about sorting in the Java Tutorials. Under Interfaces, where it says Object Ordering, and it shows some examples of Comparable and Comparators.
 
Martin Traedholm
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for the help, I manage to solve my problem, the part with the Comparator lead me in the right direction.

Have a nice weekend.
 
Campbell Ritchie
Marshal
Posts: 56223
171
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!