Results 1 to 4 of 4
  1. #1
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Problem with LinkedList

    I am working on an example in sams teach yourself java in 21 days and I have run into a problem, it has me creating an Item class which contains a name, id, quantity, and prices, then it implements comparable.

    In the next part of the program it allows me to add instances of item to a LinkedList, the definition in the book declares the linkedlist as:
    Java Code:
    LinkedList catalog = new LinkedList();
    when I try and compile I get warnings and I believe it should be:
    Java Code:
    LinkedList<Item> catalog = new LinkedList<Item>();
    I went through and changed the necessary items to work like that, and my problem comes with the sort method it defines, here is the code:

    Java Code:
    import java.util.*;
    
    public class StoreFront{
    	private LinkedList<Item> catalog = new LinkedList<Item>();
    	public void addItem(String id, String name, String price, String quant){
    		Item it = new Item(id, name, price, quant);
    		catalog.add(it);
    	}
    	public Item getItem(int i){
    		return catalog.get(i);
    	}
    	public int getSize(){
    		return catalog.size();
    	}
    	public void sort(){
    		Collections.sort(catalog);
    	}
    }
    The item object used is here:
    Java Code:
    package store;
    import java.util.*;
    
    public class Item implements Comparable{
    	private String id;
    	private String name;
    	private double retail;
    	private int quantity;
    	private double price;
    	
    	Item(String idIn, String nameIn, String retailIn, String quantityIn){
    		id = idIn;
    		name = nameIn;
    		retail = Double.parseDouble(retailIn);
    		quantity = Integer.parseInt(quantityIn);
    		if(quantity < 400){
    			price = retail * .5d;
    		}
    		else if(quantity > 200){
    			price = retail * .6d;
    		}
    		else{
    			price = retail * .5d;
    		}
    		price = Math.floor(price * 100 + .5)/ 100;
    	}
    	public int compareTo(Object obj){
    		Item tmp = (Item)obj;
    		if(this.price < tmp.price){
    			return 1;
    		}
    		else if(this.price > tmp.price){
    			return -1;
    		}
    		return 0;
    	}
    	public String getId(){
    		return id;
    	}
    	public String getName(){
    		return name;
    	}
    	public double getRetail(){
    		return retail;
    	}
    	public int getQuan(){
    		return quantity;
    	}
    	public double getPrice(){
    		return price;
    	}
    }
    the problem comes with the sort method, I get the following warning:
    Java Code:
    storefront.java:17: warning: [unchecked] unchecked method invocation: <T>sort(ja
    va.util.List<T>) in java.util.Collections is applied to (java.util.LinkedList<st
    ore.Item>)
                    Collections.sort(catalog);
                                    ^
    1 warning
    I am very unfamiliar of the generics concept, but I have some basic understanding using sets, maps, and lists.

    My question really comes down to what am I missing?

    I tried creating the linked list as:
    Java Code:
    Collection<Item> catalog = new LinkedList<Item>();
    which caused errors with the getItem method.
    Last edited by sunde887; 01-20-2011 at 06:25 AM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You are getting warnings, not errors, and the important thing to know is that if you don't want to use generics yet, you can ignore the warnings (or use annotations to get rid of them). Otherwise if you want to use generics in this code, you'll want your Item class to implement Comparable<Item>, and then change the compareTo method to take an Item parameter rather than an Object parameter.

  3. #3
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    Awesome, thanks very much for such a quick response

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

Similar Threads

  1. LinkedList
    By [RaIdEn] in forum New To Java
    Replies: 7
    Last Post: 10-13-2009, 01:59 AM
  2. LinkedList help
    By jigglywiggly in forum New To Java
    Replies: 6
    Last Post: 09-19-2009, 08:24 AM
  3. LinkedList problem
    By Mika in forum New To Java
    Replies: 7
    Last Post: 02-18-2009, 03:10 PM
  4. Problem with LinkedList Java
    By paul in forum Advanced Java
    Replies: 2
    Last Post: 08-07-2007, 05:57 AM
  5. Problem with LinkedList
    By Eric in forum Advanced Java
    Replies: 1
    Last Post: 07-05-2007, 07:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •