Results 1 to 7 of 7
  1. #1
    kiregad is offline Member
    Join Date
    Mar 2010
    Posts
    23
    Rep Power
    0

    Default How can I implement the String's length() method?

    How can I implement the length() method on char arrays?
    This is out of curiosity. I just started with java so I am researching as much as I can :)
    Java beginner.

  2. #2
    DagnusMaximus is offline Member
    Join Date
    Mar 2010
    Posts
    1
    Rep Power
    0

    Default

    Just use your char[] variableName.length()
    e.g.
    If your array was char[] toys;

    lengthOfCharArray = toys.length();

    Hope this helps

  3. #3
    kiregad is offline Member
    Join Date
    Mar 2010
    Posts
    23
    Rep Power
    0

    Default

    I am aware of that many classes have already implemented length() methods so we can use them. But I want to implement it myself.
    Java beginner.

  4. #4
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by kiregad View Post
    I am aware of that many classes have already implemented length() methods so we can use them. But I want to implement it myself.
    Your question is confusing. You can implement a length() method on any class that you write:
    Java Code:
    public class MyThing {
        private int thisProperty;
        private String thatProperty;
    
        public MyThing() {
            // TODO: whatever
        }
    
    [COLOR="Green"]    public int length() {
            int result = 0;
            // TODO: whatever you want
            return result;
        }
    [/COLOR]}
    ...but the String class is already written. Maybe if you describe the class you want to write, we can be more helpful.

    -Gary-

  5. #5
    kiregad is offline Member
    Join Date
    Mar 2010
    Posts
    23
    Rep Power
    0

    Default

    I thought "to implement" meant "to write code for". So when I am implementing the selection sort, I am writing the code for selection sort. Is this correct?

    Back to my question. I have made my own class
    Java Code:
    public class MyString {
      private char[] chars;
       public MyString(char[] chars) {
         this.chars = chars;
      }
    }
    And here I want to write a method for getting the length of MyString. Or more correctly, the length of the char array which is defined at the top.

    I want to write my own code, and make a length() method out of it.
    Java beginner.

  6. #6
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Arrays have a length field, not a method as #2 might have suggested. You can use this in your length() method:

    Java Code:
    public int length() {
        if(chars == null) {
            // throw exception (IllegalStateException?) or do something else appropriate
            // or omit this block and let it throw an NPE
            // document what you do
        }
        return chars.length;
    }

    Notice that arrays of chars are not strings. Well, you can think of them as strings in some general sense but char[] and String differ in various ways ... including length. The length() of a String is the number of characters in the sequence that the String represents while the length of a char[] instance is defined once and for all when the instance is created and is independent of the array's contents.

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Quote Originally Posted by kiregad View Post
    I thought "to implement" meant "to write code for". So when I am implementing the selection sort, I am writing the code for selection sort. Is this correct?
    Yes, that's correct.
    Quote Originally Posted by kiregad View Post
    Back to my question. I have made my own class
    Java Code:
    public class MyString {
      private char[] chars;
       public MyString(char[] chars) {
         this.chars = chars;
      }
    }
    And here I want to write a method for getting the length of MyString. Or more correctly, the length of the char array which is defined at the top.

    I want to write my own code, and make a length() method out of it.
    In this case, you would need to do something like this:
    Java Code:
    public class MyString {
        private char[] chars;
        private int len;
    
        public MyString(char[] chars) {
            this.chars = chars;
            this.len = chars.length;
        }
    
        public int length() {
            return this.len;
        }
    }
    I know that seems like it can't be right -- we didn't implement anything, right? We just kicked the can down the road, getting the length property from the array and storing it. But that really is the only way to do it. Even in C, when you create a new array, you have to keep track of its size yourself. (C strings are different, because they are null-terminated, and you can count characters until you get to the null. I suppose you could implement null-terminated strings in Java too, but why would you want to?)

    I suppose if getting the length property from the char[] really bothered you, and if you really wanted to, you could do something like this:
    Java Code:
        public int length() {
            int i = 0;
            while (true) {
                try {
                    char c = chars[i];
                    i++;
                } (catch ArrayIndexOutOfBoundsException e) {
                    return i;
                }
            }
        }
    -Gary-

Similar Threads

  1. implement binary search method using recursion?
    By chopo1980 in forum New To Java
    Replies: 1
    Last Post: 12-12-2009, 04:58 PM
  2. Replies: 1
    Last Post: 01-14-2009, 06:19 PM
  3. what does num.length method does?
    By kris09 in forum New To Java
    Replies: 1
    Last Post: 08-07-2008, 11:19 PM
  4. Integer length
    By jithan in forum New To Java
    Replies: 1
    Last Post: 06-12-2008, 04:35 PM
  5. Help with method length
    By toby in forum New To Java
    Replies: 1
    Last Post: 07-25-2007, 09:29 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
  •