I created an array, and I am trying to pass it to a method then double the size of that array. Than I would like to print the array. I am at a loss right now as to why it wont print, and what I am doing wrong. Any help appreciated. Thanks!

public class test {

    public static void main(String[] args) {

        int[] list = {1,2,3,4,5};

    }

     public static int[] doubleCapacity(int[] list) {
         
				
				for(int i = 0; i < list.length; i++){
         System.out.println(list);
			}
			
				return list;

I created an array, and I am trying to pass it to a method then double the size of that array. Than I would like to print the array. I am at a loss right now as to why it wont print, and what I am doing wrong. Any help appreciated. Thanks!

public class test {

    public static void main(String[] args) {

        int[] list = {1,2,3,4,5};

    }

     public static int[] doubleCapacity(int[] list) {
         
				
				for(int i = 0; i < list.length; i++){
         System.out.println(list);
			}
			
				return list;

Here's your main function. Nothing happens because your main function creates an array and does nothing with it. You never actually CALL your function, which you would need to do if you want that function to execute. Add the red line below to actually call the function.

public static void main(String[] args) {

        int[] list = {1,2,3,4,5};
        int[] list2 = doubleCapacity (list);

    }

Even if you did call your function, your function doesn't double any capacity. It just displays something without changing anything.

And that something that gets displayed isn't going to be your array elements. You never specify an array ELEMENT in your for-loop. You just specify the entire array, so you're going to get something really odd looking like this when you run it:

run:
[I@1a46e30
[I@1a46e30
[I@1a46e30
[I@1a46e30
[I@1a46e30
BUILD SUCCESSFUL (total time: 0 seconds)

when I assume you want to get this:

1
2
3
4
5

I really appreciate the help. Here is where I am now:

public class test {

    public static void main(String[] args) {

        int[] list = {1,2,3,4,5};
        int[] list2 = doubleCapacity (list);

    }

     public static int[] doubleCapacity(int[] list2) {
         
				
				for(int i = 0; i < list2.length; i++){
         System.out.println(list2[i]);
			}
			
				return list2;

Why wont list2 print "1,2,3,4,5,0,0,0,0,0"? If it doubled the size of the array, and 0 is the default value, shouldn't it print that? How do I prove the array size has been doubled? Thanks for the help guys!

I really appreciate the help. Here is where I am now:

public class test {

    public static void main(String[] args) {

        int[] list = {1,2,3,4,5};
        int[] list2 = doubleCapacity (list);

    }

     public static int[] doubleCapacity(int[] list2) {
         
				
				for(int i = 0; i < list2.length; i++){
         System.out.println(list2[i]);
			}
			
				return list2;

Why wont list2 print "1,2,3,4,5,0,0,0,0,0"? If it doubled the size of the array, and 0 is the default value, shouldn't it print that? How do I prove the array size has been doubled? Thanks for the help guys!

One, doubling the capacity of an array should have its own function. Display should be in a separate function. Makes things easier to follow. Two, you don't double the array's capacity anywhere in that function. You just display it. To do that in the function you have, you need to:

  1. Find out how many elements are in the original array.
  2. Double that.
  3. Declare a new array of that doubled size.
  4. Do a deep copy of the first array into the second array.
  5. Initialize any other elements of the larger array to 0.
  6. Return the larger array.

Thanks for the help! I finally finished, and got the result I wanted. Here it is:

public class test {

    public static void main(String[] args) {

        int[] list = {1,2,3,4,5};
        int[] list2 = doubleCapacity (list);

    }

     public static int[] doubleCapacity(int[] list) {
         
				int[] list2 = new int[10];
				System.arraycopy(list,0,list2,0,list.length);
				
				for(int i = 0; i < list2.length; i++){
            System.out.println(list2[i]);
			}
			
				return list;
			
	           }		
			
			}

may not be pretty, but it gets the job done. Couldn't have done it without you. Thanks again!

This question has already been answered. Start a new discussion instead.