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 6 of 6

Thread: Basic array list

  1. #1
    Junior Member
    Join Date
    Nov 2010
    Posts
    25
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Basic array list

    I am trying practise questions on arrays but I am stuck on a question.
    It says to make a code which will return true if the first or last number is a 6.

    So far I have:
    public boolean firstLast6(int[] nums) {
      int length = nums.length;
      if(nums[0] == 6){
      return true;
      }
      if(nums[length] == 6){
      return true;
      }
      else{
      return false;
      }
    }

    I am not sure how to get it to check the last number


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,462
    My Mood
    Hungover
    Thanks
    144
    Thanked 639 Times in 547 Posts

    Default Re: Basic array list

    Hint: If an array's length is 6, what is its last index?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

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

    Trunk Monkeey (January 24th, 2011)

  4. #3
    Junior Member
    Join Date
    Nov 2010
    Posts
    25
    My Mood
    Confused
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Basic array list

    Ah yeah I forgot that everything is one less in an array

    thanks

  5. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,462
    My Mood
    Hungover
    Thanks
    144
    Thanked 639 Times in 547 Posts

    Default Re: Basic array list

    It's not that everything is one less, and it's not just with arrays. For example, Strings of length 5 contain 5 characters, but the last character is at position 4.

    Or think about this: we're in the 21st century, so why does the year start with 20- and not 21-? Because the first century was actually the "zeroth" hundred years: Years 1-99 were the first century, so years 100-199 were the second century, etc. People actually debate when these centuries technically begin and end because we didn't have a "zeroth" year: did the 21st century begin in the year 2000 or the year 2001?

    Or on your 25th birthday, how many birthdays have you actually had? The answer is 26! People don't tend to think about their actual birth day as a birthday, but it would be your zeroth birthday. I'm not sure if that's a great metaphor or not.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. The Following User Says Thank You to KevinWorkman For This Useful Post:

    JavaPF (January 24th, 2011)

  7. #5
    Junior Member
    Join Date
    Jan 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Basic array list

    Strings of length 5 contain 5 characters, but the last character is at position 4 because String is an array of char so the last character is [4] because 5-1 = 4.
    I just wanted to make it clear.

  8. #6
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,462
    My Mood
    Hungover
    Thanks
    144
    Thanked 639 Times in 547 Posts

    Default Re: Basic array list

    Quote Originally Posted by xwid View Post
    Strings of length 5 contain 5 characters, but the last character is at position 4 because String is an array of char so the last character is [4] because 5-1 = 4.
    I just wanted to make it clear.
    Sorry, but I don't think that makes it clear. Where does it say that a String is an array of chars? A String is a string of chars, but where does it define the implementation of String to be that of an array?

    The last character is not at index four because five minus one is four. It's there because the first character is at index zero, so the second character is at index one, so the third character is at index two, so the fourth character is at index three, so the fifth character is at index four.

    My point is that the "0 index" idea is not specific to arrays. See also: Zero-based numbering - Wikipedia, the free encyclopedia
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. loading things from a text file into an array list
    By sp11k3t3ht3rd in forum What's Wrong With My Code?
    Replies: 11
    Last Post: February 6th, 2011, 02:32 PM
  2. SpinnerListModel setList(List<?> list)
    By roy epperson in forum Collections and Generics
    Replies: 4
    Last Post: November 29th, 2010, 09:30 AM
  3. Array List Doubt
    By mhnganesh in forum Collections and Generics
    Replies: 2
    Last Post: July 12th, 2010, 10:21 AM
  4. Doubt Regarding Printing an Array List
    By reeceypp in forum What's Wrong With My Code?
    Replies: 5
    Last Post: April 20th, 2010, 03:11 AM
  5. Replies: 1
    Last Post: November 22nd, 2008, 12:32 PM

Tags for this Thread