1

In the following code, I have resized an array, increased in length by 5 elements. length is displaying correctly. But when I run a loop to display the elements of the array, only initialized elements are displayed. How can I show all the elements?

package arraychallenge;

import java.util.*;


public class ArrayChallenge {

    public static void main(String[] args) {

        Scanner CountScanner = new Scanner(System.in);
        System.out.println("How many integers you have in your list? ");
        int count = CountScanner.nextInt();
        System.out.println("Enter the integers now - ");
        int[] MyArray = getIntegers(count);

        System.out.println("Unsorted Array is present here:");
        for (int i = 0; i < MyArray.length; i++) {
            System.out.println(MyArray[i]);
        }
        sortArray(MyArray);
        printArray(MyArray);

        resizeArray(MyArray, MyArray.length, 5);
        printArray(MyArray);


    }

    public static int[] getIntegers(int count) {
        Scanner MyScanner = new Scanner(System.in);
        int[] MyArray = new int[count];

        for (int i = 0; i < MyArray.length; i++) {
            MyArray[i] = MyScanner.nextInt();

        }
        return MyArray;

    }

    public static void sortArray(int[] MyArray) {


        int temp;

        for (int i = 0; i < MyArray.length; i++) {

            for (int j = i+1; j < MyArray.length; j++) {

                if (MyArray[i] > MyArray[j]) {

                    temp = MyArray[i];
                    MyArray[i] = MyArray[j];
                    MyArray[j] = temp;

                }

            }

        }

    }


    public static void printArray(int[] MyArray) {
        System.out.println("Sorted Array is present here:");
        for (int i = 0; i < MyArray.length; i++) {
            System.out.println(MyArray[i]);

        }
    }

    public static void resizeArray(int[] MyArray, int count, int increase){
        int[] tempArray = MyArray;
        MyArray = new int[count+increase];
        System.out.println("length of MyArray is now " + MyArray.length);
        for (int i = 0; i < count; i++) {
            MyArray[i] = tempArray[i];

        }

    }

}
Catchwa
  • 5,845
  • 4
  • 31
  • 57
KawaiKx
  • 9,558
  • 19
  • 72
  • 111
  • 2
    You can't modify the caller's reference to `MyArray` (your changes are local to `resizeArray`). – Elliott Frisch Apr 24 '17 at 03:59
  • 1
    Try returning `tempArray` from `resizeArray` and assigning it to `MyArray` – MadProgrammer Apr 24 '17 at 04:00
  • 1
    now in the resizeArray method you are not exactly modifying array object. What you need understand is that when you pass an object to a method you are just passing the reference to that object. So inside the resizeArray method you are creating a new array object and assign that new array object to MyArray parameter. you can change the method to return the MyArray that way you can get the newly created array. – Keaz Apr 24 '17 at 05:07

1 Answers1

1

In your resizeArray method, you are passing in MyArray. Then you are creating a new array of size 5, and assigning it to the MyArray variable, but this is not the same as the one you passed in.

Read this answer: https://stackoverflow.com/a/40523/3887715

My attempt at a good way to visualize object passing: Imagine a balloon. Calling a fxn is like tieing a second string to the balloon and handing the line to the fxn. parameter = new Balloon() will cut that string and create a new balloon (but this has no effect on the original balloon). parameter.pop() will still pop it though because it follows the string to the same, original balloon. Java is pass by value, but the value passed is not deep, it is at the highest level, i.e. a primitive or a pointer. Don't confuse that with a deep pass-by-value where the object is entirely cloned and passed. – dhackner Oct 20 '10 at 16:38

In short, you call the variable MyArray in resizeArray, but it's not the same MyArray you created in main. It's a new variable inside resizeArray that happens to have the same name. So by doing MyArray = new int[count+increase];, you are not changing your original MyArray, you are changing a new one.

If you run this:

  public class ArrayTest {
    public static void main(String[] args) {
        int[] array1 = new int[]{1,2,3,4,5};
        System.out.println("Print array in main 1:");
        printArray(array1);

        increaseArraySize(array1, 5);

        System.out.println("Print array in main 2:");
        printArray(array1);
    }

    private static void increaseArraySize(int[] array1, int size) {
        int[] tempArray = array1;

        array1 = new int[tempArray.length + size];

        for (int i = 0; i < tempArray.length; i++) {
            array1[i] = tempArray[i];
        }

        System.out.println("Print array in increaseArraySize:");
        printArray(array1);
    }

    public static void printArray(int[] MyArray) {
        for (int i = 0; i < MyArray.length; i++) {
            System.out.println(MyArray[i]);
        }
    }
}

You get:

Print array in main 1:
1
2
3
4
5
Print array in increaseArraySize:
1
2
3
4
5
0
0
0
0
0
Print array in main 2:
1
2
3
4
5

So you can see the 0s are being printed the second time the printArray method is called.

If you change the name of MyArray in resizeArray, maybe that will help you understand.

As suggested by the comments, you can have resizeArray return an array, and then assign that back to MyArray in main().

Fodder
  • 564
  • 1
  • 10
  • 23