【问题标题】:Using multiple int array as args使用多个 int 数组作为参数
【发布时间】:2012-10-19 07:51:07
【问题描述】:

问题来了: 我有 3 个 int [] 作为 args,我需要将它们传递给与它们一起使用的函数。 args[] 是:

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

}

输入值:

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

但我需要这些作为参数。

有什么想法吗? 提前致谢。

【问题讨论】:

  • 不清楚你在做什么。你如何调用/运行你的程序?
  • 我认为他只会通过调用它的主要方法来运行它。但是,他没有提到。
  • 我建议使用英文命名约定。许多人在阅读此代码时会遇到困难。最后,它也会帮助你,因为更多的人可以/将帮助你。 ;)
  • 是的,我用 3 个参数调用 main

标签: java arrays function int args


【解决方案1】:

如果我理解正确,您只想使用 3 个 args,其中每个 args 用作列表。好吧,您可以使用 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 列表。假设它会帮助你

【讨论】:

  • 在这我得到:Cannot make a static reference to the non-static methode fromJson(String, Type) from the Type Gson
  • 添加Gson gson = new Gson();
  • 看看我上面发布的内容:我使用了[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]"
  • 是的,GSON 使用 {...} 作为对象列表和 [...] 作为值列表。使用括号 [] 代替大括号 {}
  • 函数是public InputValues(int[] statica int[] inserta, int[] reservea) { 设置器是public void setinsertamount(int[] inserta) { this.insertamount= inserta; }...应该可以吧?
【解决方案2】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-14
    • 2013-06-09
    • 1970-01-01
    • 2011-01-01
    • 2012-07-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多