tags:

views:

88

answers:

4

supposed i created a linked list in class A how can class B access it? please give me some examples if any many thanks

A: 

One way is to pass the list to both classes through a constructor.

List<X> list = new LinkedList<X>();
A a = new A(list);
B b = new B(list);
parkr
+2  A: 

If the linked list is maintained by class A. You should create an interface that can be used by class B.

I can think of:

  • Add, to add to the linked list
  • Delete, to delete from the linked list
  • Replace, to replace an item
  • Lookup, to get an item from the list.
  • Length, to get the length of the list.

And there are possibly more (like an iterator) but it should suit your needs.

An other option is to create it outside class A and B and pass it to the classes at construction.

Gamecat
i am not sure how to create an interface like that.would you mind giving me an example? thanks
ltp00507
Just add the methods needed to manipulate the linked list to class A so class B can access them.
Gamecat
A: 

any other methods?

tfg
A: 

Does it have to be a linked list? if you use a different structure that you can control the memory space of, you can use shared memory nicely.

avirtuos