hi,
I used to program in C++ and constructed a linkedlist class (not a very fancy one but just enough to use the head and reference to simply add, and remove data in the list.

now i am learning java and realize that java.util.*; has a class called the linkedList which has all the add , and remove functions and more... my question is, is it true that there is no need to write a LinkedList class in java? all i need to do is use the methods of the already built LinkedList class for data structuring? or if that class acutally has some restriction that writing my own might be recommended.

anyone experienced please advise. thanks a lot.

The Collections Framework has implementations of many such data structures. You only have to write your own if you need functionality that is not available with the existing ones - and even then you can probably just use composition to delegate most of the work to an existing implementation and add whatever extra you need.

Check the API for ArrayList. It is the most commonly used

ArrayList list = new ArrayList();

list.add("asd");
list.add("fgh");
list.add("erty");

for (int i=0;i<list.size();i++) {
  String s = (String)list.get(i);
  System.out.println(s);
}

Or if you use generics (after java 1.5)

ArrayList<String> list = new ArrayList<String>(); //here you define what kind of objects you will put inside the list

list.add("asd");
list.add("fgh");
list.add("erty");

for (int i=0;i<list.size();i++) {
  String s = list.get(i); //you do not need to cast it to String like the previous example because of the way you defined the list above
  System.out.println(s);
}

Check the LinkedList in case it has methods that interest you

for (int i=0;i<list.size();i++) {
  String s = list.get(i); //you do not need to cast it to String like the previous example because of the way you defined the list above
  System.out.println(s);
}

Leave it to a Javacoder to write code in the most obtuse manner possible. (Yes, I'm being unfair :))

for (String s : list)
    System.out.println(s);

my question is, is it true that there is no need to write a LinkedList class in java?

There's no need to write one in C++, either. #include <list>

for (String s : list)
    System.out.println(s);

The idea is to learn about Lists, not to learn the different ways to iterate. I doubt that the above example would have helped in understanding how to add and get elements from lists.

There's no need to write one in C++, either. #include <list>

That I liked.

The idea is to learn about Lists, not to learn the different ways to iterate. I doubt that the above example would have helped in understanding how to add and get elements from lists.

Yeah, I know, and you're right. Please regard my comment as a nonsequitor.

thanks everyone for the input. At least now I know I don't have to spend time writing the linkedlist from scratch.

however, i have an object containing for example "name, address, phone, etc" and i put it in the linkedlist and don't know how to output the fields. (i do have the output() method in my Data class)

Data one = new Data("bob", "jackson", "123 main street", "781-345-3245"
	
LinkedList list = new LinkedList();		
list.add(one);

i tried

list.getFirst().output()

// it won't work
i tried

system.out.println(list.getFirst())  //weird output

does any one know if there is a method in the linkedlist class to output the object? if not, how can i add it to the already built linkedlist class.

thank you very much

You need to either specify the collection type with Generics

Data one = new Data("bob", "jackson", "123 main street", "781-345-3245"
	
LinkedList<Data> list = new LinkedList<Data>();		
list.add(one);
 list.getFirst().output();

or cast it to your Data type prior to calling methods on it.

Data one = new Data("bob", "jackson", "123 main street", "781-345-3245"
	
LinkedList list = new LinkedList();		
list.add(one);
Data first = (Data)list.getFirst();
first.output(); 
// or
((Data)list.getFirst()).output();

very clear and useful.. thank you very much Ezzaral

Also about the weird output:

System.out.println(list.getFirst())  //weird output

Whenever you put an object as a parameter in System.out.println the toString method of that object is called. In the above example you passed as parameter the: list.getFirst() which is a Data object. Since you didn't implement/override the toString method it used the one from its parent class Object (every class extends the Object class). So you got what the toString method of Object class returns.

So if you want to do something like this:

Data data = new Data();
System.out.println(data);

Add this to your Data class:

class Data {

public String toString() {
  return "whatever you want to be printed"; //probably the values of the variables
}

Also this will have the same result and the toString method will be called automatically:

Data data = new Data();
String s = "Data: " + data;
System.out.println(s);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.