Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: theory behind testing each element of an array.

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Location
    norcross, ga
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default theory behind testing each element of an array.

    Given an array of ints, return true if the array contains a 2 next to a 2 somewhere.

    has22({1, 2, 2}) → true
    has22({1, 2, 1, 2}) → false
    has22({2, 1, 2}) → false

    public boolean has22(int[] nums)
    { ...}

    maybe i could say...

    for (int i = 0; i <= nums.length; i++)
    {
      int twoCounter = 0;
      if (nums[i].equals(2))
         twoCounter++;
         if (nums[i + 1].equals(2))
              return true;
    }

    help me out with the rest of this.
    Last edited by helloworld922; February 5th, 2010 at 04:27 AM.


  2. #2
    Member
    Join Date
    Jan 2010
    Location
    Oxford, UK
    Posts
    30
    Thanks
    2
    Thanked 7 Times in 7 Posts

    Default Re: theory behind testing each element of an array.

    Do you read through your code systematically? Pretend you are a dumb computer, reading every line literally and doing what it says. For example, in the code you have written, what is the purpose of the variable twoCounter? You won't learn to program by getting people on a forum to write all your code for you!

    Having said that, the way I would write the code as follows:

    boolean pairoftwos = false;
     
    for (int i = 0; i < nums.length-1; i++)
    {
    	if (nums[i].equals(2) && nums[i + 1].equals(2))
    	pairoftwos = true;
    }
     
    return pairoftwos;

  3. The Following User Says Thank You to Shambolic For This Useful Post:

    etidd (February 5th, 2010)

  4. #3
    Junior Member
    Join Date
    Jan 2010
    Location
    norcross, ga
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: theory behind testing each element of an array.

    i already figured it out. thanks though.

Similar Threads

  1. Theory behind 2d Game making?
    By DarrenReeder in forum Java Theory & Questions
    Replies: 3
    Last Post: January 28th, 2010, 02:54 AM
  2. type substitution - whats the theory behind it
    By mds1256 in forum Java Theory & Questions
    Replies: 0
    Last Post: January 20th, 2010, 07:40 PM
  3. Testing the DataSource with MySQL
    By srinivasan_253642 in forum JDBC & Databases
    Replies: 0
    Last Post: January 9th, 2010, 02:23 AM
  4. How to extract a particular element details which has more references ???
    By j_kathiresan in forum Algorithms & Recursion
    Replies: 1
    Last Post: December 31st, 2009, 01:11 AM
  5. How can i point the mouse over a html element within the web browser?
    By bobomonkey in forum Java Theory & Questions
    Replies: 2
    Last Post: October 18th, 2009, 12:37 PM