2012-10-19 121 views
1

这里是问题: 我有3个int []作为参数,我需要将它们传递给与它们一起使用的函数。 的ARGS []是:使用多个int数组作为参数

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} 

至于来源的想法:

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(); 
} 

}

的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 

} 

这整个事情经过一个FOPConverter但部分正在工作。 这整个事情的作品,如果我硬编码阵列状

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

,但我需要的是ARGS。

任何想法? 在此先感谢。

+0

这是不是很清楚你在做什么。你如何调用/运行你的程序? –

+0

我想他会通过调用它的主要方法来运行它。但是,他没有提到它。 –

+0

我建议使用英文命名约定。许多人在阅读这些代码时遇到困难。最后,它也会帮助你,因为更多的人可以/将会帮助你。 ;) – brimborium

回答

1

如果我明白你的意思,你只想使用3个参数,每个参数用作列表。那么,你可以使用GSON包并传递像单个字符串这样的参数。下面是例子:

输入:

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); 
.... 
} 

正如你看到的我进入3个参数为字符串,并转换为清单int。假设它会帮助你

+0

在此我得到:'不能从类型Gson'的Json(String,Type)静态引用非静态方法 – Thevagabond

+0

add'Gson gson = new Gson();' –

+0

看看我上面发布的内容:我用'[14,14,17,17,14,12,13,11,12]'而不是'{...}' 。您的意见应该是:“[14,14,17,17,14,12,13,11,12]”“[74,34,57,67,34,42,53,61,22]”“[24 ,24,12,21,29,14,21,17,12]“ –

0

main(String args [])总是需要一个String类型的数组。它不允许你传递数组的数组,因为你的要求。所以,你可以做的是将每个数组作为逗号分隔的字符串传递,然后将每个字符串以分隔符分隔为','。你将能够检索你的数组。