• Post Reply Bookmark Topic Watch Topic
  • New Topic

set() and get() methods for partitioning elements of an arraylist  RSS feed

 
Tony Marchant
Greenhorn
Posts: 4
Eclipse IDE Firefox Browser Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everybody! I'm actually really excited about posting here, because I don't feel so embarrassed (Posting to Stackoverflow.com was like asking for advice from the Vulcan High Council )

So here we go...

I'm new to object oriented programming, my previous experiences have been in C. I was given an assignment to implement an operation to transform a list of data by creating a partitioning algoritm. I think I have a good start, but I'm not sure how to finish this process:



I want to grasp the this concept in greater detail, then I can use the Java API to figure out the syntax! :-D Thanks for the help Ranchers!
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66307
152
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch!

And this:
Tony Marchant wrote:(Posting to Stackoverflow.com was like asking for advice from the Vulcan High Council )

is the best (and funniest) description of SO I've heard!
 
Campbell Ritchie
Marshal
Posts: 56578
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Marchant wrote: . . . I'm new to object oriented programming, my previous experiences have been in C. . . .
Oh good grief! People who have learnt C which is procedural often have no end of difficulty understanding OO programming.

I have changed your comments to /* &hellip */ style because they made the lines too long, and got rid of some of the blank lines which do not add to the legibility of the code.

I would suggest you write out the algorithm first, without trying to write any code. See if you can get that into pseudo‑code. If you get to that stage, writing code is usually easy.

I think implementing an algorithm is not always the best way to learn OO programming. You spend lots of time and effort on the algorithm and little creating objects. Then you find that your method takes all its information in via its parameters and any information returned goes via its return type. So it becomes a 1368 in the most dubious classification of methods known to modern science. So you can work out that you don't need to create any objects at all; since you used the word static, you have obviously realised that already. So you learn all about algorithms and do little OO programming.
Does line 22 (new numbering) actually compile? If it ever does, it will not do what you think it does.

Please:-
  • 1: tell us what the algorithm is supposed to do. Is it some sort of quick sort?
  • 2: let us see the algorithm and its pseudo‑code.
  • 3: leave out coding until you have got to that stage.

  • Warning about sorting Lists: look at this sort method and you will find the warning there.

    And welcome again
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Tony Marchant wrote:I want to grasp the this concept in greater detail, then I can use the Java API to figure out the syntax!

    Well, just a small point: it's a good idea to make methods as self-contained as possible so, for example, your swap() method will be much more generic if you make it:
    public static void swap(List<String> data, int i, int j) { ...

    And just FYI, your prof is wrong: you don't need a temp field, because List.set() returns the previous value at the location, so:
    data.set( i, data.set(j, data.get(i)) );
    will do a swap (assuming both i and j are valid indexes). It is a bit of a mouthful though, so you might be better off using one.

    Winston
     
    Tony Marchant
    Greenhorn
    Posts: 4
    Eclipse IDE Firefox Browser Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thank you for the responses Winston, Campbell and Bear! First off, for Campbell's request of me.

    1) I know that for my project, the partition algorithm is In Place, and I read that this means that the memory locations are used repeatedly during this process (that's what it sounds like). The algorithm is supposed to partition "big key" values from the "small key" values, leaving the smallest integer values to the left of a pivot value. Once the values are partitioned, the modified, in place list is returned. My professor explained to me in office hours, that sorting this list of data is not important!

    2) Ok the algorithm seems like this.



    3) Ok got it!
     
    Tony Marchant
    Greenhorn
    Posts: 4
    Eclipse IDE Firefox Browser Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Winston Gutkowski wrote: Well, just a small point: it's a good idea to make methods as self-contained as possible so, for example, your swap() method will be much more generic if you make it:
    public static void swap(List<String> data, int i, int j) { ...

    And just FYI, your prof is wrong: you don't need a temp field, because List.set() returns the previous value at the location, so:
    data.set( i, data.set(j, data.get(i)) );
    will do a swap (assuming both i and j are valid indexes). It is a bit of a mouthful though, so you might be better off using one.

    Winston


    Whoa so that's totally legal in Java? Geez... I think to play devil's advocate, my Professor is sure that these awesome styles exist, but she probably wants me to validate my logic more than anything else ( but Winston, best believe I'm going to jot this in my notebook )
     
    Winston Gutkowski
    Bartender
    Posts: 10575
    66
    Eclipse IDE Hibernate Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Tony Marchant wrote:I think to play devil's advocate, my Professor is sure that these awesome styles exist, but she probably wants me to validate my logic more than anything else...

    I suspect so too; and she's in good company.

    Winston

    PS: I broke up some of your code lines because they were too long, which makes your thread very hard to read.
     
    Tony Marchant
    Greenhorn
    Posts: 4
    Eclipse IDE Firefox Browser Ruby
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Phew, well thanks everyone a few days ago I figured out a solution to the problem my Prof posted! So here it goes:




    Now, I want to know, is there a simpler way of accomplishing the partitioning? (Maybe an alternative to get() and set() ? )
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!