Thevagabond
Thevagabond

Reputation: 323

Using multiple int array as args

Here is the problem: I have 3 int [] as args and I need to pass them to the function that works with them. The args[] are:

 1: {14,14,17,17,14,12,13,11,12}
 2: {74,34,57,67,34,42,53,61,22}
 3: {24,24,12,21,29,14,21,17,12}

As for source idea:

public class Main {
    public static void main(String[] args) {
        System.out.println("amount: " + args.length);
        int[] intArray = new int[args.length];
        for(int i=0;i<args.length;i++)
             intArray[i]=Integer.valueOf(args[i]);
        int[] statica= intArray[0];
        int[] inserta= intArray[1];
        int[] reservea= intArray[2];

    GraphicConsts.getSvgStylTotalamount();
    InputValues inputValues = new InputValues(statica, inserta, reservea);
    inputValues.init();
}

}

the inputValues:

public class InputValues {

private int[] staticamount; 

public int[] insertamount;

private int[] reserveamount;

private int[] totalamount=new int[]{};
private int[] depositDF=new int[]{};
private int[] depositelse=new int[]{};

public InputValues(int statica, int inserta, int reservea) {

// TODO Auto-generated constructor stub
}
    public void init() {

// AUTO generated setters und getters

}

This whole thing goes through a FOPConverter but that part is working. This whole thing works, if I hardcode the arrays like

private int[] staticamount= new int[]{14,14,17,17,14,12,13,11,12};  

but I need those to be the args.

Any ideas? Thanks in advance.

Upvotes: 1

Views: 299

Answers (2)

Maxim Shoustin
Maxim Shoustin

Reputation: 77910

If I understand you right you want to use only 3 args where each arg used as list. Well, you can use GSON package and pass arguments like single strings. Here is example:

Inputs:

arg1: [14,14,17,17,14,12,13,11,12]

arg2: [74,34,57,67,34,42,53,61,22]

arg3: [24,24,12,21,29,14,21,17,12]

import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
....
public static void main(String[] args) {

....

Type collectionType = new TypeToken<int[]>(){}.getType();

int[] festmengeNew1 = gson.fromJson(args[0], collectionType); 
int[] festmengeNew2 = gson.fromJson(args[1], collectionType); 
int[] festmengeNew3 = gson.fromJson(args[2], collectionType); 
....
}

As you see I entered 3 parameters as String and converted to list of int. Suppose it will help you

Upvotes: 1

Sumit Desai
Sumit Desai

Reputation: 1770

main(String args[]) always requires an array of type String. It does not allow you to pass array of array, as is your requirement. So,what you can do is pass each array as a comma separated string, then split each string with delimiter as ','. You will be able to retrieve your array.

Upvotes: 0

Related Questions