View Full Version : im curious
EscapeCharacter
08-02-2002, 10:49 PM
in c/c++ if you want to dynamically create objects you use linked lists to keep track of them. so i was looking over my java book and there doesnt seem to be anything like this since java doesnt have pointers. what do people in the java world use to accomplish this?
Bradmont
08-03-2002, 12:59 AM
errm, how about java.util.LinkedList?
Java doesn't have pointers, true, but it has references, which are almost as good, and can do most of the same stuff.
jemfinch
08-03-2002, 01:10 AM
Originally posted by EscapeCharacter
in c/c++ if you want to dynamically create objects you use linked lists to keep track of them. so i was looking over my java book and there doesnt seem to be anything like this since java doesnt have pointers. what do people in the java world use to accomplish this?
You can use any data structure you like to "keep track of" dynamically allocated objects, as long as you free them before they're entirely unreferenced.
Jeremy
EscapeCharacter
08-03-2002, 01:32 AM
neat i'll have to play with that
Danger Fan
08-03-2002, 04:35 PM
In a somewhat unrelated topic, one of my projects is to create a hash table (no, we're not allowed to use the hash table class). We're using linked lists to handle collisions. There's some pretty neat stuff going on. Things like when the garbage collector runs, everything in the hash table has to be rehashed, because the garbage collector moves things around in memory to allow large chunks of free memory, instead of fragmented bits.
okay, bub-bye :wave:
Bradmont
08-03-2002, 10:27 PM
don't use lists for colisions, use trees. :)
sicarius
08-06-2002, 10:46 AM
I don't see how running the garbage collector would have any bearing on your hash table. This is just my immediate reaction based on the lack of details, so I could be wrong.
Dru Lee Parsec
08-06-2002, 12:45 PM
one of my projects is to create a hash table (no, we're not allowed to use the hash table class).
Don't you hate it when CS teachers give you these "Write this code, but don't use the obvious solution that already exist in the language".
I suppose if this is a data structures class then I could see the point. To be honest, I think I'd teach a data structures class in C++ so the students would have to use pointers and build their own linked list, hashtables, etc. Then, they could do their algorithms class or advanced study classes in Java (or some other language) that has a full set of collection classes (Vector, ArrayList, Hashtable, HashMap, Enumeration and so on). This way, they'd know (from writing it in C++) what the underlieing code is doing as they're using these data structures.
Danger Fan
08-06-2002, 01:55 PM
The hash table is for my object oriented design class. It was really easy to write it. But it would be nice if we got a project that didn't have an easy, obvious solution to it already.
Bradmont
08-06-2002, 03:34 PM
Dru: umm... C++ does have a full set of collection classes...
Dru Lee Parsec
08-06-2002, 03:50 PM
Even though I knew in the back of my mind that C++ had those collections, it's been so long since I've written C/C++ code that I totally forgot about all the changes (STL and so on) that have been made in the past several years to C++. So you're right, you can continue to use C++ with it's collections.
I just meant that it's probably a good idea to use "raw" C/C++ to write your own collection type classes to learn how they work, and then use the predefined collection classes in your real work.
My Gosh! I just realized that back when I was writing C++ code MSVC didn't even support exception handling. That was, let's see, Visual C++ version 1.5 . Damn I feel old! It feels strange to think that I use to make my living writing code in a language I barely remember any more.
_underdog
08-07-2002, 05:52 PM
Sort of along the same subject... There are different collections classes that have the same type of behavior. For example there are ArrayLists and Vectors, HashTables and HashMaps. I want to use best programming practices. I was just curious. Which classes should I favor and when might I want to use one type instead of another? I mainly do Servlet programming so I am curious about how these relate to thread safe code and performance.
Dru: ug! You know with some MFC stuff (CFileException), you still have to use the MS Macros for exception handling? Wtf is up with that!?
Dru Lee Parsec
08-08-2002, 12:36 PM
ArrayList and HashMap are faster than Vector and Hashtable, but they arn't thread safe. All that means is that if you have multiple threads adding or getting data from your collections you should wrap it with a "synchronize" wrapper.
kmj: How about this for a difference: In Java if you have a JList you simply add objects to the list and the objects know how to display themselves. When you click on the JList it returns the object you clicked on
In MFC (or at least this is how you use to have to do) you have to build a linked list (or some collection) to hold your objects. Then you figure out how to display each object and put that String into the list. If you click on the list it returns the index of the object that got clicked on and then you have to go back to your collection and use that index to get the actual object.
I dunno if they fixed that in the newer versions of MFC but you gotta ask yourself: "How NON object oriented is that?"
You STILL have to handle exceptions with macros? That's broken!
_underdog
08-08-2002, 12:43 PM
So let me make sure I have this straight. I probably should use an ArrayList instead of a vector and when I access it I should synchronize is like:
ArrayList list = new ArrayList();
synchronize(list){
list.add("data");
}
or something like that?
Dru Lee Parsec
08-08-2002, 01:56 PM
Yes, but you only need to worry about the synchronization if you're building a multi-threaded program. If you're not then just use ArrayList in place of Vector and HashMap in place of Hashtable.
Servlets are always multi-threading (each session runs on a different thread) But you still may not have to synchronize your collections. The only time you would have to synchronize is if the objects are sharing data between different sessions.
_underdog
08-08-2002, 02:58 PM
So it is safe to use an object that is not synchronized if it is local to a specific sevlet instance, but access to an object that might be shared such as an object having session or application scope should be synchronized. In that case is it safe to just use a synchronized class such as a Vector and not have any synchronize blocks?
Dru Lee Parsec
08-09-2002, 02:07 PM
In that case is it safe to just use a synchronized class such as a Vector and not have any synchronize blocks?
Yes it is. However, I want to make you aware of another little "gotcha".
If you happen to be using Apache and Tomcat AND if the thing you are saving in a collection is an HttpSession object then Tomcat does some things behind your back that will beat you up.
Tomcat wraps a Session object with it's own SessionFacade object. The object that the SessionFacade object is pointing to changes as Tomcat reuses the Facades. (Oh, they do the same thing with Request and Response objects as well).
This means that if you want to save a session, request, or response into a collection it may very well happen that when you go back to get that object one of 3 things will occur:
1: It's the object you expected it to be.
2: It's now pointing to a diffferent object.
3: It's now pointing to a null.
What this means is that if you want to save data from a session, request or response AND if you're using Tomcat then you need to pull the data out of the session, request, or response object and put it into an object of your own creation. Then you put THAT NEW OBJECT into your collection. I've found this to be the only way to assure that your data will be where you expect it to be.
vBulletin® v3.7.0, Copyright ©2000-2009, Jelsoft Enterprises Ltd.