This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Using class Iterable and sorting ArrayList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using class Iterable and sorting ArrayList" Watch "Using class Iterable and sorting ArrayList" New topic
Author

Using class Iterable and sorting ArrayList

mike karl
Greenhorn

Joined: Mar 21, 2013
Posts: 13
i have to write a method setOrder() which takes enumeration constant defined in Report so iterating over Bills is performed in the order specified by this constant and i have have no clue where to start.
this is what i have so far, let me know what i did wrong and what i should do next.

fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

mike karl wrote:i have to write a method setOrder() which takes enumeration constant defined in Report so iterating over Bills is performed in the order specified by this constant and i have have no clue where to start.
this is what i have so far, let me know what i did wrong and what i should do next.

What you did wrong was to start writing code without thinking about what you should do.

The single most important thing a programmer needs to do is NOT WRITE CODE. Before committing a single byte to file, you should think through the problem in very NON-java terms. you should pretend you are explaining to a child what needs to be done.

Only when you've done that, revised it and simplified it several times, should you write any actual code.

So I would say your next step is to delete what you have, turn off your computer, and put pencil to paper.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
mike karl
Greenhorn

Joined: Mar 21, 2013
Posts: 13
fred rosenberger wrote:
mike karl wrote:i have to write a method setOrder() which takes enumeration constant defined in Report so iterating over Bills is performed in the order specified by this constant and i have have no clue where to start.
this is what i have so far, let me know what i did wrong and what i should do next.

What you did wrong was to start writing code without thinking about what you should do.

The single most important thing a programmer needs to do is NOT WRITE CODE. Before committing a single byte to file, you should think through the problem in very NON-java terms. you should pretend you are explaining to a child what needs to be done.

Only when you've done that, revised it and simplified it several times, should you write any actual code.

So I would say your next step is to delete what you have, turn off your computer, and put pencil to paper.


is it really that badly done, this is my second semester in programming and my teacher doesnt explain much, im not sure how i can revise this if im not even sure what this is, i never used iterators. i never set up a enumerator to perform sorting. i need some helpful advise
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7518
    
  18

mike karl wrote:is it really that badly done,

Fraid so; although I think I understand what you were attempting to achieve.

this is my second semester in programming and my teacher doesnt explain much, im not sure how i can revise this if im not even sure what this is, i never used iterators. i never set up a enumerator to perform sorting. i need some helpful advise

And you've already received it. Most of us have BTDTGTTS, so unless you haven't shown any effort (which you plainly have), we're not trying to belittle you.

The fact is that Fred's absolutely right, you've decided on an implementation (your code) before you've understood the problem - and that's always a bad sign - and now you're in the position of trying to defend the indefensible.

My advice: StopCoding (←click) before you have too much invested in this to turn back.

There are plainly some good ideas in your attempt; it just happens to be wrong.

Here's a possibility for you: Have your enum (one of your good ideas) implement java.util.Comparator<Bill>. Have a think about it and see if it makes any sense.

Winston

Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

mike karl wrote:

This certainly looks wrong; it's an unbounded recursive loop. The method iterator() calls itself, which calls itself, which calls itself, which calls itself, etcetera, until it crashes with a StackOverflowError.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
mike karl
Greenhorn

Joined: Mar 21, 2013
Posts: 13
Winston Gutkowski wrote:
mike karl wrote:is it really that badly done,

Fraid so; although I think I understand what you were attempting to achieve.

this is my second semester in programming and my teacher doesnt explain much, im not sure how i can revise this if im not even sure what this is, i never used iterators. i never set up a enumerator to perform sorting. i need some helpful advise

And you've already received it. Most of us have BTDTGTTS, so unless you haven't shown any effort (which you plainly have), we're not trying to belittle you.

The fact is that Fred's absolutely right, you've decided on an implementation (your code) before you've understood the problem - and that's always a bad sign - and now you're in the position of trying to defend the indefensible.

My advice: StopCoding (←click) before you have too much invested in this to turn back.

There are plainly some good ideas in your attempt; it just happens to be wrong.

Here's a possibility for you: Have your enum (one of your good ideas) implement java.util.Comparator<Bill>. Have a think about it and see if it makes any sense.

Winston


ok thanks ill see what i can do, just understand i am in Poland and they dont explain well enough, i feel like i am being rushed through alot of material but maybe its just me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using class Iterable and sorting ArrayList
 
Similar Threads
Big performance hog with (Collection).removeAll ?
For-eaching an iterator
Simple ArrayList conundrum?
Doubt i a generics question
abstract classes