Results 1 to 3 of 3
Like Tree1Likes
  • 1 Post By jim829

Thread: If statement.

  1. #1
    ssk2659 is offline Member
    Join Date
    Jan 2015
    Posts
    48
    Rep Power
    0

    Default If statement.

    Why is in the code, in if statement i!=j??

    Java Code:
    public static boolean allUnique(int[] xs){
    	  
    	  for (int i = 0; i < xs.length; i++)
    	  {
    		  for(int j = i; j < xs.length; j++)
    			 if(i != j && xs[i] == xs[j])
    				 return true;	  
    	  }
    	  return false;
      }

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: If statement.

    Quote Originally Posted by ssk2659 View Post
    Why is in the code, in if statement i!=j??
    I guess you didn't write this since you are asking about the logic. The name implies that the method is determining that a supplied array has no duplicate values. So I recommend you try a 5 element array on paper with no duplicate and one duplicated value and work thru the logic yourself, recording i, j, x[i] and x[j]. It will be much more rewarding than me telling your the answer. But if you get stuck, ask a question.

    Note: Based on my assumption about what the method does, I believe the author of this code got the return values reversed.

    Regards,
    Jim
    Last edited by jim829; 01-31-2015 at 10:30 PM.
    ssk2659 likes this.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    14

    Default Re: If statement.

    BTW, there is a way to do this so you don't need the i != j test.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Replies: 5
    Last Post: 05-18-2014, 04:41 AM
  2. Statement and prepared statement
    By suhaas.mohandos@gmail.com in forum JDBC
    Replies: 10
    Last Post: 01-30-2014, 02:44 PM
  3. Replies: 3
    Last Post: 11-19-2012, 09:12 PM
  4. Replies: 2
    Last Post: 03-26-2012, 05:27 PM
  5. Statement or Prepared Statement ?
    By paty in forum JDBC
    Replies: 3
    Last Post: 08-01-2007, 05:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •