Results 1 to 8 of 8
- 10-13-2012, 02:16 AM #1
Member
- Join Date
- Apr 2012
- Posts
- 21
- Rep Power
- 0
Generic List I've made holding Objects I've made must be cast?
I'm taking a Data Structures class and it is in Java. Right now I am working on making my own linked list. Everything seems to be working properly (no type warnings)
~Again, note that this is not the java.util List~
I have a List<E>, used in main:
This works as expected. However, when I use my own data type:Java Code:List<Integer> myList = new List<Integer>(); for (int i = 0; i < 5; i++) myList.push_back(i); } System.out.println("Welcome to my list: "); //ListIterator I have made for (ListIterator iter = myList.getIterator(); iter.exists(); iter.next()) System.out.println(iter.get());
That last line gives me an error: incompatible typesJava Code:String str = "Hello World. Nice to meet you"; String[] splitStr = str.split(" "); List<MyData> myList = new List<MyData>(); for (int i = 0; i < splitStr.length; i++) myList.push_back(new MyData(splitStr[i], i)); } System.out.println("Welcome to my list: "); //ListIterator I have made for (ListIterator iter = myList.getIterator(); iter.exists(); iter.next()) System.out.println(iter.get().number);
I can fix the error like so:
But I don't want to have to do that. When I just do:Java Code:for (ListIterator iter = myList.getIterator(); iter.exists(); iter.next()) { MyData temp = (My Data) iter.get(); System.out.println(temp.number); }
I get MyData@memAddress printed on screen, so the program does know that it is a MyData already?Java Code:for (ListIterator iter = myList.getIterator(); iter.exists(); iter.next()) { System.out.println(iter.get()); }
If it helps, my iter.get() function:
I guess I'm looking to find out where the disconnect is happening that I would need the cast.Java Code:public E get() { return iter == null ? null : iter.data; }
Thanks for reading/helping :)
- 10-13-2012, 02:48 AM #2
Re: Generic List I've made holding Objects I've made must be cast?
First off, you really shouldn't name your own classes the same as existing Java classes. It's really confusing and can lead to other problems.
But to answer your problem, you have to pass the generics data into your Iterator, as well. Right now you aren't supplying it, so it's defaulting to Object.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 10-13-2012, 03:10 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 21
- Rep Power
- 0
Re: Generic List I've made holding Objects I've made must be cast?
Aha, thank you very much.
The names are confusing. I will think about a new name.
- 10-13-2012, 04:48 PM #4
Member
- Join Date
- Apr 2012
- Posts
- 21
- Rep Power
- 0
Re: Generic List I've made holding Objects I've made must be cast?
Okay, so now I have another problem, sorting.
Since Objects can not use the comparisson operators, we must use the compareTo() method. I've made myself a List<Integer>, which I assume should work find (indeed Integer does implement Comparable). However, I'm getting the "can not find symbol error":
Which is fixable via:Java Code:// my name is Carl for (first = new CarlListIterator<E>(head); first.myNode.next != null; first.next()) { for (second = new CarlListIterator<E>(first.myNode.next); second.exists(); second.next()) { if ( first.myNode.data.compareTo(second.myNode.data) > 0) { swap(first.myNode, second.myNode); CarlListIterator<E> temp = first; first = second; second = temp; } } }
I realize for any Object I make I will need to have some comparrison functions (actually, I hope to make my sort accept an interface). I'm wondering how I can get my list to work with Java's types?Java Code:for (first = new CarlListIterator<E>(head); first.myNode.next != null; first.next()) { for (second = new CarlListIterator<E>(first.myNode.next); second.exists(); second.next()) { if ( ((Integer)first.myNode.data).compareTo((Integer)second.myNode.data) > 0) { swap(first.myNode, second.myNode); CarlListIterator<E> temp = first; first = second; second = temp; } } }
Thanks again.
Edit: Maybe I've only described half the error message. The message reports that "first.myNode.data" and "second.myNode.data" are of different types: E#1 and E#2. Not quite sure about this.Last edited by Lowest0ne; 10-13-2012 at 04:58 PM.
- 10-13-2012, 06:26 PM #5
Re: Generic List I've made holding Objects I've made must be cast?
That's a wonky use of a for loop- most people would use a while loop with an iterator, but hey whatever works.
Your CarlListIterator contains Objects of type E- but what does it know about E? Is E guaranteed to implement Comparable?How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 10-13-2012, 08:22 PM #6
Member
- Join Date
- Apr 2012
- Posts
- 21
- Rep Power
- 0
Re: Generic List I've made holding Objects I've made must be cast?
For now, let's just say yes.Is E guaranteed to implement Comparable?
I've cleaned things up a little, no longer getting an error that they are two different types. Making CarlList<Integer>:
I get the error: Can not find symbol compareTo() in method compareTo(E).Java Code:public class CarlList<E> { //... Many other things public void sort() { // Cases to abort if (size < 2) return; ListIterator<E> first; ListIterator<E> second; int i = 0; for (first = new ListIterator<E>(head); first.myNode.next != null; first.getNext()) { for (second = new ListIterator<E>(first.myNode.next); second.exists(); second.getNext()) { if (first.get().compareTo(second.get()) > 0) { swap(first.myNode, second.myNode); ListIterator<E> temp = new ListIterator<E>(first.myNode); first = second; second = temp; } } } } }
location: Class Object
where E is a type-variable: E extends Object declared in class CarlList
Which again, is solved:
The get() returns an E, but we aren't quite seeing that E is an Integer?Java Code:if (((Integer)first.get()).compareTo((Integer)second.get()) > 0)
- 10-14-2012, 01:06 AM #7
Re: Generic List I've made holding Objects I've made must be cast?
What I mean is, you might know that E is going to be Comparable, but how does the compiler know? Recommended reading: Bounded Type Parameters (The Java™ Tutorials > Learning the Java Language > Generics (Updated))
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 10-14-2012, 06:05 AM #8
Member
- Join Date
- Apr 2012
- Posts
- 21
- Rep Power
- 0
Re: Generic List I've made holding Objects I've made must be cast?
Thanks for the link, now I have something like this:
And things are working fine.Java Code:public class CarlList <E extends Comparable<e>>
So the answer is now certainly yes. Which means MyData needs a "sorting mode" that will tell it how compareTo() should work. Not the most elegant solution, perhaps you can toss something out and I'll see how far over my head it goes?Is E guaranteed to implement Comparable?
Thanks a whole lot. :)
Similar Threads
-
A game made by me.
By utsav1995 in forum Reviews / AdvertisingReplies: 0Last Post: 05-30-2012, 07:41 PM -
Why is it saying I have made an illegal expression?
By Ampzor in forum New To JavaReplies: 3Last Post: 02-08-2012, 03:16 PM -
Collections sorting a list made of generic class objects
By andreiutz10 in forum New To JavaReplies: 5Last Post: 02-07-2012, 05:56 PM -
Use a dll made in .net (C#) COM Visible within JAVA
By ealopez26 in forum Advanced JavaReplies: 6Last Post: 09-16-2011, 04:46 AM -
No class made
By ChuckLS in forum New To JavaReplies: 1Last Post: 04-28-2009, 04:54 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks