Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use Array Initializers in a Function in Java

I want to write a function which gives me a different int array testArray as an output according to the input I choose. In this example, I want to choose number = 0, and get testArray = {1,2,3,4,5} as a result. When I give testArray values, I get this error:

Array constants can only be used in initializers

public class ArrayExample{  
    
    public static void main(String args[]){
        int number = 0;
        int[] newArray = new int[5];
        newArray = getValues(number);
    }
    

    public static int[] getValues(int number) {
        
        int[] testArray = new int[5];
            
            if(number == 0) {
                testArray  = {1,2,3,4,5}; 
            }
            else if(number == 1) {
                testArray  = {2,3,4,5,6}; 
            }
            else if(number == 2) {
                testArray  = {3,4,5,6,7}; 
            }
            else if(number == 3) {
                testArray  = {4,5,6,7,8}; 
            }       
            else{
                testArray  = {5,6,7,8,9}; 
            }   

            
        return testArray;
    }

    
}
like image 572
user6210457 Avatar asked Jul 05 '26 11:07

user6210457


2 Answers

Try it like this using Arrays.setAll

for (int i = 0; i < 5; i++) {
    int [] result = getValues(i);
    System.out.println(Arrays.toString(result));
}

prints

[1, 2, 3, 4, 5]
[2, 3, 4, 5, 6]
[3, 4, 5, 6, 7]
[4, 5, 6, 7, 8]
[5, 6, 7, 8, 9]

The method

public static int[] getValues(int number) {
    int[] array = new int[5];
    Arrays.setAll(array, i->number+i+1);
    return array;
}

You could also pass the size of the array as an argument.

like image 181
WJS Avatar answered Jul 08 '26 01:07

WJS


You can use the syntax {1, 2, 3, 4, 5} only in an initializer (An initializer is the code used to define a new variable and assign to it a value). You need to use the syntax new int[]{1, 2, 3, 4, 5} if you are not using an initializer.

You can refactor your code as follow:

public static int[] getValues(int number) {
   switch (number) {
       case 0: return new int[]{1,2,3,4,5}; 
       case 1: return new int[]{2,3,4,5,6}; 
       case 2: return new int[]{3,4,5,6,7}; 
       case 3: return new int[]{4,5,6,7,8}; 
       default: return new int[]{5,6,7,8,9}; 
   }
}

Note that I also used a switch that is more readable for this situation. But you can still use an if else if else if you like more.

You can also change the main's method follwing code

 int[] newArray = new int[5];
 newArray = getValues(number);

to

 int[] newArray = getValues(number);

infact you don't need to create an empty array that you don't use (new int[5]) and you can directly assign the array returned by the getValues method.

like image 40
Davide Lorenzo MARINO Avatar answered Jul 08 '26 01:07

Davide Lorenzo MARINO



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!