d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Constructor
Add Reply New Topic New Poll
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Jun 26 2012 08:45am
I don't really understand the third constructor at all. Like how to do it, could anyone explain?

Code
public class SortableLinkedList<T extends Comparable<T>> implements Iterable<T> {
private Node<T> head;
private Comparator<T> comp;
private int listSize;



public SortableLinkedList(Comparator<T> comp) { //creates an empty list with the associated comp
 head = null;
 this.comp = comp;
}


 *  method defines a comparator based on the compareTo method associated
 * with T. You must use "this" to call the previous constructor

public SortableLinkedList() {
 this.comparator = new Comparator<T>(){
  @Override
  public int compare(T o1, T o2) {
   if(o1.compareTo(o2) == 0){
    return 0;
   }else if(o1.compareTo(o2) < 0){
    return -1;
   }else{
    return 1;
   }
  }
 };
}

 * Creates a list based on the elements present in the array list.  You may not use
 * the list iterator to implement this method.

public SortableLinkedList(Comparator<T> comp, ArrayList<T> L) {
 
       ???????????????????????????????????????

}


This post was edited by lopelurag on Jun 26 2012 09:04am
Member
Posts: 649
Joined: May 28 2012
Gold: 14.00
Jun 26 2012 08:56am
I would say you have first to pur the first element of the arraylist at head then use the comparator to compare elements of your arraylist with head and nexts to sort correctly the list
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Jun 26 2012 09:03am
Quote (FCNantes @ Jun 26 2012 10:56am)
I would say you have first to pur the first element of the arraylist at head then use the comparator to compare elements of your arraylist with head and nexts to sort correctly the list


How do you compare the elements tho, this is what I got so far...

Code

public SortableLinkedList(Comparator<T> comp, ArrayList<T> L) {
if(L == null){
  head = null;
 }
 int index = 0;
 head = (Node<T>) L.get(0);
 Node<T> curr = head;
 while(index < L.size()){
  curr.next = (Node<T>) L.get(index);
  index++;
 }
 
 while(index < L.size()){
  L.get(index);
 }
}


This post was edited by lopelurag on Jun 26 2012 09:06am
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jun 26 2012 10:41am
in pseudocode, i think he wants you to do something like this:
Code
this(comp);
head = list.get(0);
node = head;
for i = 1 to n{
  node.next = list.get(i);
  node = node.next;
}


then ofc later on you use your compartor to actually sort the linked list.

This post was edited by carteblanche on Jun 26 2012 10:41am
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Jun 26 2012 10:53am
Quote (carteblanche @ Jun 26 2012 12:41pm)
in pseudocode, i think he wants you to do something like this:
Code
this(comp);
head = list.get(0);
node = head;
for i = 1 to n{
  node.next = list.get(i);
  node = node.next;
}


then ofc later on you use your compartor to actually sort the linked list.


Word ty mate
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll