0

I am trying to change the size of a String array, but I don't know what else to try and this method isn't working. Here is my code:

import java.util.Scanner;

public class test{
    public static void main(String[] args){
        String[] names;
        names = new String[5];
        for(int i=0; i<10; ++i){
            addToArray(names, i, "blah");
        }
        System.out.println(names.length);
        System.out.println("bye");
    }

    public static void addToArray(String[] names, int i, String name){
        if (i < names.length){
            names[i] = name;
        }
        else {
            String[] temp;
            temp = new String[names.length + 10];
            System.arraycopy(names, 0, temp, 0, names.length);
            addToArray(temp, i, name);
            names = temp;
        }
    }
}

My problem is that when I run it, it prints that the final length of my array is 5, when it should say 15.

2
  • possible duplicate of Resize an Array while keeping current elements in Java? Commented Feb 19, 2014 at 4:37
  • @user2310289 nope. I looked at that, my effort uses the suggested System.arraycopy. I wasn't very specific with my problem, but when I print the length of the array it is 5, and I expect it to be 10. Commented Feb 19, 2014 at 4:39

4 Answers 4

5

The problem is, you can't change the reference of any of your (Object) method arguments, so...

names = temp;

Only changes the reference of the names variable within the context of the addToArray method. You should use a return statement instead...

public static String[] addToArray(String[] names, int i, String name){
    if (i < names.length){
        names[i] = name;
    }
    else {
        String[] temp;
        temp = new String[names.length + 10];
        System.arraycopy(names, 0, temp, 0, names.length);
        names = addToArray(temp, i, name);
    }
    return names;
}

Don't forget to maintain the reference once the method returns...

for(int i=0; i<10; ++i){
    names = addToArray(names, i, "blah");
}
Sign up to request clarification or add additional context in comments.

Comments

2

Once an array has been created, the size of that array cannot be changed. An array can hold a fixed amount of objects once created.

An ArrayList however is able to change the amount of objects it holds dynamically. A way to initialize a simple ArrayList would be:

ArrayList<String> stringArrayList = new ArrayList<>();

6 Comments

He's not changing the size of the existing array, he's creating a new one with a larger size, copies elements into it and then makes that temporary array the new array. It's just his referencing is a bit off... he needs to use a return.
While I would always agree, we need more context into why the OP is choosing to dynamically resize arrays manually. Besides, this is almost exactly what an ArrayList does any way, it's just backed by an array which is resized as needed ;)
I'm doing this manually because I have no experience in Java at all, lol. My main language is Python :p . I'm drawing mostly on my C experience to learn this though.
@Broseph In that case, the suggestion by Brian is an excellent one, you should take the time to have a read through the Collections tutorial - +1 to Brian
Having the right tools can make a job a lot easier.
|
1

Three points

  1. Arrays are special objects in java, they have a simple attribute named length which is final.
  2. Also you are creating a new array, not re-sizing the original array.
  3. you can't change the reference of any of your method arguments

2 Comments

Well, he's not exactly trying to change the size of the existing array. He already demonstrated the basic understanding of what you've stated, so he's trying to create a new array and point to it.
Yes, it's much better.
0

change the return type as String[] and return names in addToArray() method and assign names=addToArray(String[] names, int i, String name); in mainMethod thus you can maintain the keep tracking with reference when size increases

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.