File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes An easy question about swap Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "An easy question about swap" Watch "An easy question about swap" New topic
Author

An easy question about swap

Cheryl Scodario
Ranch Hand

Joined: Nov 28, 2009
Posts: 69
Hi all,
As I was coding today, there was a problem I ran into when I was making my swap method. It contains a 'set' method which is to set an element/value to the index/position i of the array. The wrong code is the following:


Here is the correct code:



My question is I can't tell the difference between the two. The second/correct code just assigns temp to get(i). It's like naming get(i); give get(i) a name. Please explain the difference to me!

Another quick question is about array. So when we want to create an array, we usually have a format like this: String [] input = new String[52];
But must you use new to create an array? Because I think the following is also valid:
input[i] = reader.readLine();
String[] parts = input[i].split(" ") Here, we didn't new this parts array? We just make it to point to another existing array.

Thanks for all the help!
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Cheryl Scodario wrote:...String[] parts = input[i].split(" ") Here, we didn't new this parts array? We just make it to point to another existing array...

No, this is not assigning parts to an existing array.

input is an array. But the element, input[i], is a String. And calling the split method on a String creates and returns a new array.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4529
    
    5

The first code says:
get the value at j and set it at i
get the value at i and set it at j
Once you've set the value at i, getting it will get the new value you've just set. You've already lost the old value that was at i before you set it.

About your second question:
Here, we didn't new this parts array? We just make it to point to another existing array.

No, the new array is created in the String#split(...) method. From the source:So you see an array is always created in the same way, although that may not be in your own code.

luck, db
There are no new questions, but there may be new answers.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

When you start...
  • Index i holds element i
  • Index j holds element j


  • In the "wrong" code, you have...

    After this line runs...
  • Index i holds element j
  • Index j holds element j

  • ...so element i is lost (and the next line of code makes no difference).

    The correct code avoids losing element i by storing it in a temporary variable.
    marc weber
    Sheriff

    Joined: Aug 31, 2004
    Posts: 11343

    I think this is already clear from the response above, but it's Monday and I'm home from work and thought of a better way to explain it (using colored text ), so...

    The WRONG CODE loses its reference to element i.

    You start with:

  • Index i: Element i
  • Index j: Element j


  • Run the line...

    set(i, get(j));

    ...and you have...

  • Index i: Element j
  • Index j: Element j


  • At this point, the damage is done, because element i is no longer referenced. Then when you run the line...

    set(j, get(i));

    ...it just reassigns element j (which is now also referenced at index i) to index j, and you still have...

  • Index i: Element j
  • Index j: Element j



  • The CORRECT CODE keeps a handle on element i by assigning it to a temporary variable.

    As before, you start with:

  • Index i: Element i
  • Index j: Element j


  • But first, put a reference to element i into a temp variable for safe keeping.

    Object temp = get(i);

    So now you have...

  • temp: Element i
  • Index i: Element i
  • Index j: Element j


  • Now, run the line...

    set(i, get(j));

    ...and you have...

  • temp: Element i
  • Index i: Element j
  • Index j: Element j


  • As before, element i is no longer referenced in the array, but you still have a handle on it with the temp variable, which you can now use for the swap...

    set(j, temp);

    ...resulting in...

  • temp: Element i
  • Index i: Element j
  • Index j: Element i
  • fred rosenberger
    lowercase baba
    Bartender

    Joined: Oct 02, 2003
    Posts: 11230
        
      16

    Imagine you have two pieces of paper, each holding an address. One holds ivan's address, and one holds john's. when you say this:

    this.set(i, get(j));

    you say "erase what is on ivan's paper, and write down the address from john's piece of paper instead." After doing this, you don't have ivan's address anymore, since you just erased it.

    then you say

    this.set(j, get(i));

    this says "erase what is on john's piece of paper, and write down the address from ivan's piece of paper", which at this point contains john's address.


    There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
    Cheryl Scodario
    Ranch Hand

    Joined: Nov 28, 2009
    Posts: 69
    Darryl Burke wrote:
    // Pattern.java

    public String[] split(CharSequence input, int limit) {
    :
    : // for certain conditions
    return new String[] {input.toString()};

    :
    : // for other conditions
    String[] result = new String[resultSize];
    return matchList.subList(0, resultSize).toArray(result);
    }[/code]So you see an array is always created in the same way, although that may not be in your own code.


    Thanks, Darryl. I see it now, so in the split method, it has already "new" the array implicitly (not visible to the human eye) for String[] parts.
    Cheryl Scodario
    Ranch Hand

    Joined: Nov 28, 2009
    Posts: 69
    marc weber wrote:When you start...
  • Index i holds element i
  • Index j holds element j


  • In the "wrong" code, you have...

    After this line runs...
  • Index i holds element j
  • Index j holds element j

  • ...so element i is lost (and the next line of code makes no difference).

    The correct code avoids losing element i by storing it in a temporary variable.


    Hey marc! Your first explanation has already made perfect sense! Thanks a lot for attempting the second time! I got it. This is really elementary java knowledge, and I should have mastered it by now! I will remember that for future reference!
     
    jQuery in Action, 2nd edition
     
    subject: An easy question about swap