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 9232381
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:18:04+00:00 2026-06-18T06:18:04+00:00

I am trying to create a Boolean method to resize my array and return

  • 0

I am trying to create a Boolean method to resize my array and return true if widening or false for narrowing!
here is what I have done so far, please help me i don’t know what tod od for the rest:)

public class StringArray implements InterfaceStringArray{

String[] strArray; // create an array & store value
private int nElems =0;  // number of elements
final int ARRAY_MAX = 100;



    StringArray bubbleSort= new StringArray();     // create bubbleSort object
    StringArray selectSort = new StringArray();    // create selectSort object
    StringArray insertSort = new StringArray();   // create insertSort object


public void initialize (int arrSize) { //initializes an array of size=arrSize
    strArray = new String [arrSize]; 
}


public int getSize() { //returns the current array size
    return strArray.length;
}




public boolean resize(int newSize) { //returns true if successful (widening), false if narrowing
    String[] newArray = new String[ strArray.length * 2];

    for ( int x=0; x < strArray.length; x++){ // Load array
        if (newArray[x].equals(strArray[x]) )
        return true;
    }
    return false;
}
  • 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-18T06:18:06+00:00Added an answer on June 18, 2026 at 6:18 am

    OK , even though using ArrayList < String > would be better for your task, I’ll just try to stick to your current implementation and re-write the resize() method.

    /*
     *  A method to resize the array.
     *  @param  : newSize - new required size of array
     *  @return : true if widening, false if narrowing
     *
     */
    public boolean resize(int newSize) { 
    
        boolean toReturn = true;
    
        // if narrowing or the size isn't being changed, return value changes to false; 
        if ( newSize <= strArray.length ){
             toReturn = false;
        }
    
        String[] newArray = new String[newSize];
    
        // yes, Java allows creating zero-sized arrays
    
        if(newSize != 0){
    
           // copying from old array to new one; it doesn't matter if this is narrowing 
           // or widening, everything that fits in will be copied
           System.arraycopy(strArray,0,newArray,0,newSize);
    
           // copying pointer to newArray to the variable where strArray used to be
           strArray = newArray;
    
        }
        return(toReturn);
    
    }
    

    What your current implementation does is absolutely different from your goal. First, you are doubling your array size instead of setting it with parameter newSize. Secondly, your method proceeds like this :

    • create a double sized array;
    • iterate through the main array and in each step compare String in strArray with String in newArray;
    • as soon as the Strings on position x are equal, return true;
    • if none are equal, return false;

    Note: it will always return false, because newArray is empty.

    Some reading about ArrayLists:

    • docs.oracle.com;
    • http://web.cs.dal.ca/~sedgwick/ArrayList.html
    • javarevisited.blogspot.ru;
    • Collections/CreatinganArrayList.htm”>http://www.java2s.com/Tutorial/Java/0140_Collections/CreatinganArrayList.htm
    • http://www.tutorialspoint.com/java/java_arraylist_class.htm;

    Unfortunately the best books about Java that I know are written in Czech, so I can’t advise anything unless you speak that language…

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

Sidebar

Related Questions

I'm trying to create a Dictionary with array of integer, string and boolean data
In a method I created I am trying to create is meant to return
I'm trying to create an array in one method (or function? Or object? Side
I am trying create a delegate representation of constructor by emitting a Dynamic Method,
Ok so I am trying create a login script, here I am using PHP5
im trying to create a method which allows me to check if a BST
I am trying to create a Response class in Java which has a method
I am trying to create a data structure to hold polynomials. So far i
I am trying to create helper method that would iterate through all Bundle objects,
The following code is how I'm trying to create a recursive binary method.. public

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.