Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2007

Tip: Looking for answers? Try searching our database.

What Data Structure should I use?

Thread view: 
Abhishek - 12 Dec 2007 08:48 GMT
Hello,

  I need to use a linked list, but each node of a linked list need to
be a structure like I use in C/C++.
may I know what data structure is suitable for such a construction?
Each node of the double linked list has to be a structure with its own
fields like Char, String and Integer.

Thanks
Abhishek
Sabine Dinis Blochberger - 12 Dec 2007 09:42 GMT
> Hello,
>
[quoted text clipped - 6 lines]
> Thanks
> Abhishek

You would define your own class and instantiate objects for each element
in your linked list.

Also have a look at Java collections [1] to replace your linked list if
applicable.

[1] http://java.sun.com/docs/books/tutorial/collections/index.html
Signature

Sabine Dinis Blochberger

Op3racional
www.op3racional.eu

Abhishek - 12 Dec 2007 11:13 GMT
On Dec 12, 2:42 pm, Sabine Dinis Blochberger <no.s...@here.invalid>
wrote:
> > Hello,
>
[quoted text clipped - 18 lines]
>
> Op3racionalwww.op3racional.eu

Thanks Sabine. The first suggestion suits me guess.
Thanks
Abhishek - 12 Dec 2007 11:45 GMT
On Dec 12, 2:42 pm, Sabine Dinis Blochberger <no.s...@here.invalid>
wrote:
> > Hello,
>
[quoted text clipped - 18 lines]
>
> Op3racionalwww.op3racional.e

I have another doubt now.

I have a function called foo() and it accepts objects of classes as
parameters.
how do I define the parameter in the function definition?
foo( Object obj) ?

it can be called as foo(Ftr)
or foo(Xyz) where Ftr and Xyz are objects of different classes.
Sabine Dinis Blochberger - 12 Dec 2007 12:16 GMT
> On Dec 12, 2:42 pm, Sabine Dinis Blochberger <no.s...@here.invalid>
> wrote:
[quoted text clipped - 30 lines]
> it can be called as foo(Ftr)
> or foo(Xyz) where Ftr and Xyz are objects of different classes.

Hope I'm not putting my foot in my mouth here (but someone is bound to
correct me or give a better suggestion <g>).

Assuming the objects and their handling are distinclty different, I
would define two methods, and each accepts a different parameter:

 public void foo(Ftr paramname) { ... }
 public void foo(Xyz paramname) { ... }

Signature

Sabine Dinis Blochberger

Op3racional
www.op3racional.eu

Abhishek - 12 Dec 2007 15:11 GMT
On Dec 12, 5:16 pm, Sabine Dinis Blochberger <no.s...@here.invalid>
wrote:
> > On Dec 12, 2:42 pm, Sabine Dinis Blochberger <no.s...@here.invalid>
> > wrote:
[quoted text clipped - 44 lines]
>
> Op3racionalwww.op3racional.eu

Hey, I myself found a better method and it works.

You really do not need to define two different function.
You can declare it as foo(Object ParameterName,....)
Yes, the datatype of the ParameterName is written as "Object", which
is the superclass of any class object.
Inside you can find out which object has been passed by using
ParameterName.getClass().toString()  which actually gives you the Name
of the class to which this object (ParameterName) belongs to in form
of a string.
Once you know which class' object has been passed, you now are
literally inside that class.

Thanks for your suggestion though. I am not sure if what I have used
breaks down under any special case, but works fine as of now :-)

Thanks
Abhishek S
Lew - 12 Dec 2007 15:20 GMT
> Hey, I myself found a better method and it works.

Actually, it's a worse method, in that you sacrifice type safety and manually
do inside your code what the compiler and runtime would have done for you,
thus increasing code complexity and the likelihood of bugs, and reducing the
ability to add functionality later.

> You really do not need to define two different function.
> You can declare it as foo(Object ParameterName,....)

You can, but that's an antipattern.

> Yes, the datatype of the ParameterName is written as "Object", which
> is the superclass of any class object.

And thus completely eliminates all the benefits the language would have given
wrt type safety.

> Inside you can find out which object has been passed by using
> ParameterName.getClass().toString()  which actually gives you the Name
> of the class to which this object (ParameterName) belongs to in form
> of a string.

Or you could just call getClass() on the argument inside the method, and save
the redundantly repetitive overhead of the extra parameter, but it would still
be an antipattern.

> Once you know which class' object has been passed, you now are
> literally inside that class.

The runtime always knows which class was passed.  Your hack adds nothing.

> Thanks for your suggestion though. I am not sure if what I have used
> breaks down under any special case, but works fine as of now :-)

The case need not be special for it to break down.

Generics might also provide some relief for your situation.

Signature

Lew

Abhishek - 12 Dec 2007 17:21 GMT
> > Hey, I myself found a better method and it works.
>
[quoted text clipped - 37 lines]
> --
> Lew

Thanks Lew. Those suggestions seem to make a lot of sense.
Can you provide me with a good framework here please?

Thanks
Abhishek
Mark Space - 12 Dec 2007 20:02 GMT
> Thanks Lew. Those suggestions seem to make a lot of sense.
> Can you provide me with a good framework here please?

Lew did mention Generics.  I think that's probably the best idea.

public class MyFoo<T> {
  public void foo( T parameterName ) {
    // ... do foo stuff
  }
}

Then when you use it, you parameterize the type (I think I have that
terminology right).  You mentioned Char, String and Integer.  Examples:

MyFoo<Char> charFoo = new MyFoo<Char>();
MyFoo<String> stringFoo = new MyFoo<String>();
MyFoo<Integer> integerFoo = new MyFoo<Integer>();

Pretty simple, less work, and type safe.  No disadvantages that I can
see unless you're required to use J2ME or something where these new
features may not be available.

Note: not syntax checked or compiled.  And Char isn't a Java type. It's
Character. ;-)  But you could declare a Char if you wanted.
Abhishek - 13 Dec 2007 12:06 GMT
> > Thanks Lew. Those suggestions seem to make a lot of sense.
> > Can you provide me with a good framework here please?
[quoted text clipped - 21 lines]
> Note: not syntax checked or compiled.  And Char isn't a Java type. It's
> Character. ;-)  But you could declare a Char if you wanted.

Thanks for those ideas. I am in a situation I am stuck inside with.
I have a List (LinkedList) which has is a list of objects of a
particular class.
How Do I access the member variables of any particular object in that
List?

I am not able to do
Class_Name cls = List_Objects.get(0).Var_Name;
List_Objects.get(0) actually should give me the first object in the
list right?

Thanks
Abhishek
Lew - 13 Dec 2007 15:44 GMT
> Thanks for those ideas. I am in a situation I am stuck inside with.
> I have a List (LinkedList) which has is a list of objects of a
[quoted text clipped - 4 lines]
> I am not able to do
> Class_Name cls = List_Objects.get(0).Var_Name;

Because this doesn't make sense.

> List_Objects.get(0) actually should give me the first object in the
> list right?

Yes.  But Object doesn't have a member "Var_Name", so it makes no sense to
refer to it.

Let's start by suggesting that you adhere to the Java naming conventions to
make the examples clearer.  Class names start with an upper-case letter,
variable and method names start with a lower-case letter.  Eschew underscores.

Don't put implementation ("List") into variable names.

List foos = new LinkedList(); // or new ArrayList() or ...

You didn't show generics, though you should have, so I'm going raw for a moment.

foos.add( new Foo( "first" ));
foos.add( new Foo( "second"));
etc.

later,

Foo foo = (Foo) foos.get( 0 );
String name = foo.getName();

The problem is, someone might add() a non-Foo to the List.  Then the class
cast would blow up.  Generics to the rescue:

List <Foo> foos = new LinkedList <Foo> ();
foos.add( new Foo( "first" ));
foos.add( new Foo( "second"));
etc.

Now you can skipo the class cast, because the compiler won't let a non-Foo
into the list.

Foo foo = foos.get( 0 );
String name = foo.getName();

BGTW, LinkedList isn't as fast at a get() as other types of List.
> Operations that index into the list will traverse the list
> from the beginning or the end, whichever is closer to the specified index.
<http://java.sun.com/javase/6/docs/api/java/util/LinkedList.html>

Signature

Lew

Abhishek - 13 Dec 2007 18:30 GMT
> > Thanks for those ideas. I am in a situation I am stuck inside with.
> > I have a List (LinkedList) which has is a list of objects of a
[quoted text clipped - 53 lines]
> --
> Lew

Thanks Lew. That was a great piece of advice.
I have made the necessary changes into my code.
Now what datastructure other than the list do you think is suitable if
I have thousands of Objects of type foo to feed inside?
I did read that List is quite slow and starts indexing from the
beginning or end. May I know whats the fastest?

With Regards,
Abhishek S
Lew - 14 Dec 2007 02:25 GMT
> I did read that List is quite slow and starts indexing from the
> beginning or end. May I know whats the fastest?

Huh?  List isn't "quite slow".  Where ever did you read such a thing?

Some implementations of List are slower at some things than others.  It
doesn't make List a bad choice for what you want to do.  It doesn't even make
LinkedList bad for what you want to do.  It depends on what you want to do.

What do you want to do?

> I have thousands of Objects of type foo to feed inside ...

I have no clue what "feed inside" means, nor how you intend to traverse the list.

And that should be type "Foo", by convention, not "foo".

Signature

Lew
This post contains requests for TWO pieces of information.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.