• Help me with Java? Trying to code the link between two different type ArrayLists
    7 replies, posted
[url]http://pastebin.com/m68c5655e[/url] - CarSort [url]http://pastebin.com/m750169d1[/url] - Car I'm missing the link between tmp and array ArrayLists, so my sorting methods aren't running correctly, if at all. Run the program, enter a few snippets of data, then try sorting to get the error, and see what I mean. Help needed, PLEASE! I've been at this for days.
Didn't run your code, but you do need to have your Car class implement the Comparable interface for sorting to work correctly. [editline]01:08PM[/editline] Why are you keeping the VINs separate from the Cars? Set up your car, store it in a list, keep that list, and then later pass the whole ArrayList of Car to a sort method. Then let the sort method worry about getting the VIN from the car and sorting by that. Or, better yet, do what I said before and implement Comparable, and define compareTo. Like this: [cpp] public class Car implements Comparable< Car > { ... public int compareTo( Car that ) { if( this.VIN == that.VIN ) return 0; return ( this.VIN < that.VIN ) ? -1 : 1; } } [/cpp]
Ok, I've added what you've said to add. I previously kept the VINs seperate from the Car ArrayList because the of the way it saves data. If I entered: dodge ram 5346 That info would be stored in the Car ArrayList in the [0] index, the VIN obviously being the last thing at that index. I didn't know how to take that data and sort by just vin. So I figured I'd take the vin in seperately, and that's how I coded the program. But know the problem is that I have to figure out another way to put the VINs back in to the Car ArrayList in sorted order, and I'm having trouble doing that. I know it would be easier if I kept everything in that one array, I just don't know exactly how to go about that. I'm leaving for the day, and taking my netbook with me: I'll work on it while I'm gone if I have the time. But could anyone explain to me the best solution for sorting the data by VIN, and keeping it in one ArrayList in the meantime? So when I get back, I can compare what I have, or haven't accomplished and then fix it. This is really a learning experience for me. Thanks in advance.
As long as your Car class implements Comparable< Car >, and your sort methods utilize the compareTo method correctly, you should just be able to change your method signature to take a ArrayList< Car > instead of ArrayList< Integer >, then change your calling code to make sure you pass in the correct ArrayList (this should be obvious as the compiler will complain after you change your method signature) [editline]03:40PM[/editline] Just looked in the API, and Java has a really nice method for this stuff. [url]http://java.sun.com/javase/6/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator[/url]) This will let you sort any List with your own comparator. Like this: [cpp] final Comparator< Car > carComparator = new Comparator< Car >() { @Override public int compare( Car c1, Car c2 ) { if( c1.getVIN() == c2.getVIN() ) return 0; return ( c1.getVIN() < c2.getVIN() ) -1 : 1; } @Override public boolean equals( Object o ) { return false; // change this if you need to - see the API docs } } List< Car > cars = new ArrayList< Car >(); // this list holds all of your cars ... // populate your list java.util.Collections.sort( cars, carComparator ); // sort it with your Comparator [/cpp]
Now coding a my own comparator for the task seems like the right thing to do, at least to me. But then I am given these instructions that contradict that solution: [quote=Assignment Details] [i]Objective:[/i] Create a program that will perform three sorts on a set of data: Merge Sort, Shell Sort and Quick Sort. The set of data will be an ArrayList of Cars and you will need to create the Car class yourself. You will also time each of the executions of these sorts and print out the time it took for each of these sorts. I will provide you the code for the timing. [i]Expectations:[/i] Your code will need to be neat, concise, well documented and above all, correct (see Testing). [i]The Car Class:[/i] The car class will contain three fields: make, model and vin. Make and model are Strings and vin is an integer. Make sure that you include get and set methods for all three of these as well as a useful constructor. [b]You do not need to code your own comparator.[/b] You will compare two cars by their vin number only. [i]Testing:[/i] You will need to provide some sort of input functionality that allows me to input make, model and vin as many times as I desire. Note that this includes 0. I will test your program with invalid inputs as well so make sure to catch any errors. Also, I will test it using 0 cars. Each of these inputs will be used to populate the ArrayList. [/quote] Dare I ask, what is the way I am expected to compare two vins?
I still think implementing Comparable< Car > in your car method is the best bet. Then just call the compareTo method in your sorting method.
I wouldn't use Comparable, actually, because that would mean that the VIN is the single default way to sort cars, to the exclusion of all others, such as by make, model, or year. It'd work for the purposes of this assignment since that's all you need to sort by, but it's poor design practice IMO because it hard-codes an assumption (that cars are typically sorted by VIN) that isn't really true or appropriate. If you point to a parking lot and instruct someone to "sort these cars", the person probably won't think to go looking at the VIN labels. They'll probably give you a blank stare, actually, and ask "what does it mean to 'sort' cars?" That's the hint that a Car class shouldn't really have a built-in default comparison function. Instead, I'd write a CarVinComparator class, which implements Comparator<Car>, and pass an instance of that into the sorting methods. (Comparator is a lot like Comparable, except that it's used on a separate class rather than the one being compared.) That'd leave the door open for other types of comparators (e.g. CarMakeComparator, CarModelComparator) to be used with the same sorting methods and the same list of cars. BTW, did you see the reply that I posted yesterday in your other thread?
[QUOTE=Wyzard;18390678]I wouldn't use Comparable, actually, because that would mean that the VIN is the single default way to sort cars, to the exclusion of all others, such as by make, model, or year. It'd work for the purposes of this assignment since that's all you need to sort by, but it's poor design practice IMO because it hard-codes an assumption (that cars are typically sorted by VIN) that isn't really true or appropriate. If you point to a parking lot and instruct someone to "sort these cars", the person probably won't think to go looking at the VIN labels. They'll probably give you a blank stare, actually, and ask "what does it mean to 'sort' cars?" That's the hint that a Car class shouldn't really have a built-in default comparison function. Instead, I'd write a CarVinComparator class, which implements Comparator<Car>, and pass an instance of that into the sorting methods. (Comparator is a lot like Comparable, except that it's used on a separate class rather than the one being compared.) That'd leave the door open for other types of comparators (e.g. CarMakeComparator, CarModelComparator) to be used with the same sorting methods and the same list of cars. BTW, did you see the reply that I posted yesterday in your other thread?[/QUOTE] I totally agree with your method; it seems the most logical way to leave multiple options open for sorting all kinds of cars. If I was given this assignment for my job, I would not hesitate to take your advice, but as this is a course assignment, I think I'd be better off doing the minimum: I really don't have the time to add on to this. I'm sure it would impress my professor though. And no, I haven't gotten to check my other thread yet, I've been busy most of the weekend. I'll read it right now.
Sorry, you need to Log In to post a reply to this thread.