File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

An easy question about swap

 
Cheryl Scodario
Ranch Hand
Posts: 69
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Darryl Burke
Bartender
Posts: 5115
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 11343
    Java Mac Safari
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 12022
    25
    Chrome Java Linux
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.

     
    Cheryl Scodario
    Ranch Hand
    Posts: 69
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 69
    • 0
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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!
     
    I agree. Here's the link: http://aspose.com/file-tools
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic