Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8818317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T05:04:50+00:00 2026-06-14T05:04:50+00:00

need to create a int length() method where the array is outside of the

  • 0

need to create a int length() method where the array is outside of the method . Was thinking of doing a for loop for (int i = 0; i < array.length; i++)…but im not sure help please. on API it looks easy but the array is not in the method itself so idk how to do it.

public class MyString
{

    private char[] array;
    private int size;
    private int max;

    public MyString()
    {
        array = new char[25];
        max = 25;
    }
    public void setString(String newString)
    {
     if(newString.length() > 25)
     {
         System.out.println("/nEnter a number equal or less than 25 " );

     }
     else
     {
      for(int i=0; i < newString.length(); i++)
      {
        array[i] = newString.charAt(i);
      }
     }
    }


    public String toString()
    {
        return new String(array);
    }

    public char charAt(int index)
    {
        return array[index];
    }

    public boolean contains(char ch)
    {
        for(char c: array)
        {
            if(c == ch) return true;
        }
        return false;
     }
     public int indexOf( char ch )
     {
        for (int i = 0; i < array.length; i++)
        {
           if (array[i] == ch)
           {
               return i;   // Character found, return current index
           }
         }
            return -1;    // Character not found. Return -1
      }
      public int length()
      {
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T05:04:51+00:00Added an answer on June 14, 2026 at 5:04 am

    I am not sure if I get your question completely right, but

    public int length() {
       return array.length;
    }
    

    should do it. An array in java has a length member which contains the array length.

    Note that this returns the Array length which will always be 25 in the code from your question.

    If you want the String length, either use the approach from @Grisha, or use the size member which is already in your code but not used yet:

    public MyString() {
       array = new char[25];
       max = 25;
    
       size = 0;
    }
    
    public void setString(String newString) {
       if(newString.length() > 25) {
          System.out.println("/nEnter a number equal or less than 25 " );
       } else {
          size = newString.length();
    
          for(int i = 0; i < size; i++) {
              array[i] = newString.charAt(i);
          }
       }
    }
    
    public int length() {
       return size;
    }
    

    This would avoid a loop with linear complexity (length() would be O(1) and not O(n)).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to create an Objective-C method that converts an int into a byte
I need create clone repository. but I do not know where can I get
I need a method that reads a file to a byte array asynchronously but
I need to pass a 1d array that isn't defined in a method. I
How do I create a method that takes a two-dimensional array as a parameter
I need create custom dialog and put JPanel into it. Is it possible?
i need create an email list sending to many emails. what is best solution
I need create a document word with Java. And I ask, how can I
i need create a variable with parent subclass. Example: Parent Class <?php class parentClass
I use MS SQL 2008 R2, I need create a Table with a CHECK

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.