aspose file tools*
The moose likes Cattle Drive and the fly likes Out of Sorts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » This Site » Cattle Drive
Bookmark "Out of Sorts" Watch "Out of Sorts" New topic
Author

Out of Sorts

Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
I am floundering in a major way. I have code that reads the list in from the source file, and prints it in a list. I also have code that prints the contents of my ArrayList as an Array, just to prove to myself that my ArrayList is loaded with the names from the list. The Array prints out in this format:
[element1, element2, etc...]
First thing, am I on the right track?
Second thing, when I try to use sort(List) I get an error message stating that you can't convert void to char[]. Since my ArrayList is not empty.......
I tried to use Collections as my object to call sort, but I can't instantiate a Collections object because it has no constructor. So.........
I'm kinda lost here. I can't call sort() because I don't have a Collections object, and the compiler thinks my ArrayList is void for some reason. HELP!!!
On the up side, my IO is working, at least I think it's working.
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
Originally posted by Carol Murphy:
I am floundering in a major way. I have code that reads the list in from the source file, and prints it in a list. I also have code that prints the contents of my ArrayList as an Array, just to prove to myself that my ArrayList is loaded with the names from the list. The Array prints out in this format:
[element1, element2, etc...]
First thing, am I on the right track?

No need to resort to using an Array. ArrayList implements the Collection interface and therefore it has a convenient method that iterates over the population of the list for you, so you don't have to worry about the gory details. (Actually this method is implemented by ArrayList's superclass, AbstractList.)
Second thing, when I try to use sort(List) I get an error message stating that you can't convert void to char[]. Since my ArrayList is not empty.......
I tried to use Collections as my object to call sort, but I can't instantiate a Collections object because it has no constructor.

sort() is a static method. It belongs to the class as a whole, not to any particular instance of the class. Ayou noticed, by design you cannot instantiate this class. All its methods are static. You call it like this:
Collections.sort( myList ) ;
I'm guessing that the error you're getting is because you're trying to do something like this:
char[] elements = Collections.sort( myList ) ; // doesn't work
So.........
I'm kinda lost here. I can't call sort() because I don't have a Collections object, and the compiler thinks my ArrayList is void for some reason. HELP!!!

Like I mentioned, you don't need a Collections object to call sort(). The compiler doesn't think your ArrayList is void. The sort() method "returns void." And (if my guess is correct,) you're trying to use the results of that method call in a context that is expecting an array of chars.
On the up side, my IO is working, at least I think it's working.

Well done!
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
Hi Carol,
Collections is not intended to be instantiated. All of its methods are static (class wide) so you can use them without creating a Collections object. Just use Collections.sort( yourArrayList ) ;.
Since a reference to the original list is passed to the sort method it takes care of all the details and simply provides you with the list sorted, meaning it does not return anything. The call to sort, with the list as an argument, is all that is needed. Once the list is sorted you can then use the ListIterator to print the sorted list.
One last thing -- everything can be done with the ArrayList.
Let us know if you need more info.
[This message has been edited by Richard Boren (edited July 03, 2001).]
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
Okay guys, stay with me here! I think I'm making some headway, but I have a language problem. A lot of the jargon in programming is meaningless to me. My code prints the list of names as it is being read in. Then in another part of the program I have: Collections.sort(nameofArrayList);
System.out.print(nameofArrayList.listIterator(0));
which delivers this line when I run the program:
java.util.AbstractList$ListItr@8212e99f
Exactly what is being printed by calling listIterator on my ArrayList?
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
Collections.sort(nameofArrayList);
System.out.print(nameofArrayList.listIterator(0));
which delivers this line when I run the program:
java.util.AbstractList$ListItr@8212e99f
Exactly what is being printed by calling listIterator on my ArrayList?

Well you asked -- Object.toString() where the object in question is an instance of ListItr. See the thread at http://www.javaranch.com/ubb/Forum19/HTML/000659.html for the gory details.
But that's not very relevant to the problems you're having. The important thing here is that that's not how you use an iterator.
An iterator provides a means of passing through the elements of a collection without having to know the gory details of how the collection stores or indexes the elements.
In another thread http://www.javaranch.com/ubb/Forum19/HTML/000667.html Richard Boren pointed out the two chief methods you need to use: hasNext() and next(). And also gave the URL for the Sun Java Collection Tutorial that has examples of using iterators, along with very good info on collections in general and sorting (which you'll find in the section on "ordering").
There are 2 very typical ways of using an iterator:
Given a an instance of a Collection, c:

and

Remember that a Collection is going to store Object references, so you may need to downcast it.next() , depending on what you want to do with it.
[This message has been edited by Michael Matola (edited July 03, 2001).]
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
Hallelujah! I now have code that prints out the list in alphabetical order by first name. Thanks for your help. All these words like iterator and comparator and implementing and interface are swirling around in my head and I feel like I don't really understand what any of them mean. I think it's time to sit back and let some of this information settle somewhere. By the way, what does downcast mean? First time I've heard that one.
One more question before I hit the saloon- The objects stored in my ArrayList are Strings, right? So if I want to use String operations like indexOf() on each element, I should be able to do that, right??? Just wondering how to sort by last name. Or there's probably a method called by some arcane name which will do the trick, but I am having a hell of a time with the Java Jargon! Thanks again, off to tie one on!
Happy fourth guys!!!
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Hi Carol, happy 4th of July! I'm going to test myself here and try to explain "downcasting".
Casting in general has to do with data types, how big they are and how to get bigger types to fit into smaller ones.
The basic rule is that a bigger type has to be "cast" first in order to get it to fit into a smaller type.
Example:
Type integer uses 32 bits, type long uses 64 bits so it's bigger.
long myBigLong = ... ;
int myLittleInteger = ( myBigLong + 3 ) ; // this won't work!
myBigLong has to be "cast", that is, explicitly made to fit into an integer type:
int myLittleInteger = ( (int)myBigLong + 3 ) ; //this should work

Downcasting is casting from a bigger type to a smaller type. Upcasting is the inverse and, as I understand it, is done automatically by the compiler, which recognizes the different "sizes".
I haven't been confronted with casting objects yet (as opposed to simple primitive types like integer, etc.), but I assume the basic idea is the same: an object may be bigger than the variable you want to make it fit into, so it would need to be "downcast".

("Making it fit" actually removes bits, so depending on what's being cast, there's sometimes a danger of losing important information.)
Hope this helps...
Pauline

[This message has been edited by Pauline McNamara (edited July 04, 2001).]
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
Originally posted by Carol Murphy:
One more question before I hit the saloon- The objects stored in my ArrayList are Strings, right? So if I want to use String operations like indexOf() on each element, I should be able to do that, right???

(1) Yes.
(2) No, not without downcasting.
To add to Pauline's explanation of casting -- when you're working with objects instead of primitives, it's helpful to think of up and down along the inheritance tree.
(1) If you put Strings into an ArrayList, then those objects are still Strings. (Java objects stay the same type throughout their lifetime.)
(2) But ArrayList sees them as more generic Objects (they get "upcast" to Objects when you add them to the ArrayList). And any objects that you use from the ArrayList (whether you get at them using the get() method or an Iterator) you can interact with only through the methods provided by Object (unless you downcast to a more specific type). So your Strings are still Strings, but you can only use the methods found on Object unless you downcast them back to Strings.
For the purpose of printing out inside the loop for the Iterator, there's no issue. If you remember that
System.out.println( it.next() ) ;
is shorthand for
System.out.println( it.next().toString() ) ;
you'll have no problem. You can call toString() because Object has a toString() method. However, note that when the call gets made, since this is really a String object it's String's version of the toString() method that gets called.
(If you find this confusing, I suggest taking a look at the articles "Cup Size" and "How my Dog learned Polymorphism" at http://www.javaranch.com/campfire.jsp .)
If you try to call a String method here
System.out.println( it.next().indexOf( '-' ) ) ; // doesn't compile
you get a compiler error because it.next() resolves to an Object and you're trying to call a String method on an Object reference.
That said, at some point in your program (not here when you're looping through the Iterator), you'll need to call some String methods on these names. So given an Object reference "name" for something you want to cast back to a String, you can use this syntax
(Sting)name ;
Or if combining it with a method call
( (String)name ).indexOf( '-' ) ;
Just wondering how to sort by last name.

That's the question that got this whole conversation started. I don't think I can add anything new to what's already been said without giving too much away. But if you run into specific roadblocks, maybe folks can help.
[This message has been edited by Michael Matola (edited July 04, 2001).]
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
Michael, have you ever considered writing a text book? Your explanations are very clear! Thanks! Gotta get back to work, I'm late!
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
Oh no. No textbook writing for me.
It's code I want to write more and more of.
It's just a useful exercise for me to try to answer questions and explain what I can -- it tests my own understanding.
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
In that case, keep testing your understanding around here!
Pauline
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
Okay Michael, I've hit a wall, and I am running in circles in front of it because I can't get over it. Let me try to explain. I have code that reads in the specified list from another file and sorts it alphabetically by first name. So far so good. I instantiated my ArrayList as a static class member outside of main. All of the other code is inside my main method. I have been trying to write an inner class for SortNames which implements the desired interface so I can sort by last name. This is where I am getting bogged down. What I want to do is write code that will extract a substring from each element in my ArrayList and pass that substring into sort() as one of the parameters. I am hoping that this will cause the list to be sorted alphabetically by the substrings. At any rate, I can't figure out how to overwrite the method in the interface. I declare the objects and define them, but when I try to pass them into the method I get this message from the compiler: Identifier expected.
I don't know where to go from here. I placed my inner class definition right after the ArrayList and right before main(). Perhaps I need to adjust the placement of some of the code?
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
>I have code that reads in the specified list from another file
>and sorts it alphabetically by first name. So far so good. I
>instantiated my ArrayList as a static class member outside of
>main. All of the other code is inside my main method.

Sounds good so far.

>I have been trying to write an inner class for SortNames which
>implements the desired interface so I can sort by last name.

It doesn't have to be an inner class.
>This is where I am getting bogged down. What I want to do is
>write code that will extract a substring from each element in
>my ArrayList and pass that substring into sort() as one of the
>parameters. I am hoping that this will cause the list to be
>sorted alphabetically by the substrings.

Sounds good so far.

>At any rate, I can't figure out how to overwrite

You mean "over-ride"

>the method in the interface.

Create a class that implements the interface. Create a method in the class with the same name and parameters and return type as that method in the interface.

>I declare the objects and define them, but when I try to pass
>them into the method I get this message from the compiler:
>Identifier expected.

Sometimes something as simple as a missing curly brace will cause this message.

What objects are you declaring and initializing? Are you instantiating objects of the inner class?

>I placed my inner class definition right after the ArrayList
>and right before main(). Perhaps I need to adjust the
>placement of some of the code?

Placement seems correct for an inner class.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195


>At any rate, I can't figure out how to overwrite

You mean "over-ride"
(Oops!)
>the method in the interface.

Create a class that implements the interface. Create a method in the class with the same name and parameters and return type as that method in the interface.
Do I have to figure out a means of comparing the two objects that get passed in to this method, or can I just make an empty method?

>I declare the objects and define them, but when I try to pass
>them into the method I get this message from the compiler:
>Identifier expected.

Sometimes something as simple as a missing curly brace will cause this message.

What objects are you declaring and initializing? Are you instantiating objects of the inner class?
I am trying to create a class that gets the last names from each element in the ArrayList. I tried to instantiate an instance of my inner class, then create 2 objects to pass in to the method called on it. These 2 objects are substrings from the ArrayList. Since these substrings are initially objects, I cast them to strings to use get() then I cast the strings to objects in order to pass them into compare. This is where I'm running into trouble. I don't really know what I'm doing here, I'm just trying to create an object to pass into sort() and I can't manage to do it!
It seems I need to connect the instance of the new class with the elements in my ArrayList, but I'm just turning in circles trying to come up with a way to do that.
I think I know what I want to do, I just don't know how to accomplish it.

>I placed my inner class definition right after the ArrayList
>and right before main(). Perhaps I need to adjust the
>placement of some of the code?

Placement seems correct for an inner class.[/B]
Perhaps I can't base my new class on the elements in ArrayList. I was thinking that since the code for loading the elements into the ArrayList was inside main, I would have to be able to access some of that code to use the substrings in my other class.
Amber Woods
Ranch Hand

Joined: Mar 28, 2001
Posts: 111
Hi Carol,
I found a couple of links on the sun site in the tutorials that might help you out: http://java.sun.com/docs/books/tutorial/collections/index.html
and more specifically http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html
HTH


"Happiness is a way to Travel, <b>Not</b> a Destination" -- Unknown
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Originally posted by Carol Murphy:
I was thinking that since the code for loading the elements into the ArrayList was inside main ...


You should use a static block to initialize your ArrayList ... see the assignment for an example of a static block.
Michael Matola
whippersnapper
Ranch Hand

Joined: Mar 25, 2001
Posts: 1746
    
    2
Originally posted by Marilyn deQueiroz:
You should use a static block to initialize your ArrayList ... see the assignment for an example of a static block.

Marilyn --
Which assignment do you have in mind? Initializing the HashMap in NaturalLanguageMultiply in a static block makes sense to me, but the ArrayList in SortNames?
What's the advantage of that? (Or is figuring that out an important part of the assignment?)
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
And the plot thickens.........
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9046
    
  10
Oops
Wrong assignment
Maybe I've been looking at too many NaturalLanguageMultiply assignments lately.
[This message has been edited by Marilyn deQueiroz (edited July 10, 2001).]
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
Or maybe the holiday blues Marilyn
jason adam
Chicken Farmer ()
Ranch Hand

Joined: May 08, 2001
Posts: 1932
I think I understand what you are doing Carol, and it is what was hanging me up big time on this assignment. Take a look at the OOP-3 Sorting postings that I started (don't know how to add strings and such on here), it addresses some of this.
Take a step back and really look over the interface you are implementing, the method you are overriding in that interface, and how that relates to Collections. Also, realize that the sort being done by Collections (making sure you are using the correct sort method of course, there are two) handles everything in regards to sorting, you just have to come up with the logic to tell it whether it is sorting by first or last name. It sounds like you have the general idea, now you just need to figure out where to place the logic.
Don't give up on it though, I thought I would pull my hair out on this one, but once you understand what the interfaces, methods, and parameters are doing, it should fall into place!
Haven't been nitpicked yet, but it works!
Jason
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
Thanks Jason. ( And everyone else! ) I think I'm almost there. I know what I want to do but I am having syntax trouble. On top of that, I'm putting in a lot of overtime at work, so I haven't been able to really concentrate ( obsess? ) on this too much. I will give a holler if I need more help!
Matthew Phillips
Ranch Hand

Joined: Mar 09, 2001
Posts: 2676
A method from NaturalLanguageMultiply came in handy when coding how to handle the last names. The method made handling the syntax of the method pretty simple. It took me forever to figure out how simple. I haven't been nitpicked yet, but I am looking forward to it.
Matthew Phillips

Matthew Phillips
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
I am so confused right now that I can't even figure out how my NaturalLanguageMultiply program works, and I did that weeks ago! I look at the code I once understood and it makes no sense in reference to SortNames. I am banging my head on a brick wall, and it's not coming down!
Richard Boren
Ranch Hand

Joined: Mar 01, 2001
Posts: 233
I've got to say, the only thing I really found useful in NaturalLanguageMultiply for SortNames was the try catch block. Other than that they seemed complete different to me.
Carol Murphy
village idiot
Bartender

Joined: Mar 15, 2001
Posts: 1195
I have been working on this all afternoon and I am further away from a solution than I was before. No matter what I try, I cannot get the program to accept the parameter I am passing in. I tried to several different ways, and nothing works for me. This assignment has me thoroughly discouraged.
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
Hi Carol, sounds like SortNames is the Sayb of the OOP assignments. When I get that far, I supect I'll be wishing I could either throw the box out the window, or go out for some fresh air (for you, ocean air?!)
The second option is much less expensive!
Hang in there.
Johannes de Jong
tumbleweed
Bartender

Joined: Jan 27, 2001
Posts: 5089
No way Pauline OOP-4 is the Say of OOP !!.
but then you got a great book to help you with that one, hope you get it before I put you onto the inactive students list
Pauline McNamara
Sheriff

Joined: Jan 19, 2001
Posts: 4012
    
    6
I'm really looking forward to getting into that book. In fact, if it's as good as it promises to be, it's more likely that I'll end up on the inactive list after I get it. I love reading.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Out of Sorts