Java 使用多个int数组作为参数
问题是: 我有3个int[]作为参数,我需要将它们传递给使用它们的函数。 参数[]是:Java 使用多个int数组作为参数,java,arrays,function,int,args,Java,Arrays,Function,Int,Args,问题是: 我有3个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("amoun
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();
}
这整件事都经过一个FOPConverter,但那部分正在工作。
如果我把数组硬编码为
private int[] staticamount= new int[]{14,14,17,17,14,12,13,11,12};
但我需要那些是ARG
有什么想法吗?
提前感谢。main(字符串args[])始终需要字符串类型的数组。它不允许您按要求传递数组的数组。所以,您可以做的是将每个数组作为逗号分隔的字符串传递,然后使用分隔符“,”拆分每个字符串。您将能够检索数组。main(字符串args[])始终需要字符串类型的数组。它不允许您按要求传递数组的数组。所以,您可以做的是将每个数组作为逗号分隔的字符串传递,然后使用分隔符“,”拆分每个字符串。您将能够检索阵列。如果我理解正确,您只希望使用3个参数,其中每个参数用作列表。您可以使用GSON包并像传递单个字符串一样传递参数。下面是一个例子: 投入: arg1:
[14,14,17,17,14,12,13,11,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);
....
}
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);
....
}
import java.lang.reflect.Type;
导入com.google.gson.gson;
导入com.google.gson.reflect.TypeToken;
....
公共静态void main(字符串[]args){
....
Type collectionType=new-TypeToken(){}.getType();
int[]festmengeNew1=gson.fromJson(args[0],collectionType);
int[]festmengeNew2=gson.fromJson(args[1],collectionType);
int[]festmengeNew3=gson.fromJson(args[2],collectionType);
....
}
如您所见,我输入了3个参数作为字符串,并转换为
int
列表。假设它将帮助您如果我理解正确,您只想使用3个参数,其中每个参数用作列表。您可以使用GSON包并像传递单个字符串一样传递参数。下面是一个例子:
投入:
arg1:[14,14,17,17,14,12,13,11,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);
....
}
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);
....
}
import java.lang.reflect.Type;
导入com.google.gson.gson;
导入com.google.gson.reflect.TypeToken;
....
公共静态void main(字符串[]args){
....
Type collectionType=new-TypeToken(){}.getType();
int[]festmengeNew1=gson.fromJson(args[0],collectionType);
int[]festmengeNew2=gson.fromJson(args[1],collectionType);
int[]festmengeNew3=gson.fromJson(args[2],collectionType);
....
}
如您所见,我输入了3个参数作为字符串,并转换为
int
列表。假设它能帮助你你现在还不清楚你在做什么。你如何调用/运行你的程序?我想他只会调用它的主方法来运行它。但是,他没有提到。我建议使用英语命名惯例。许多人在阅读此代码时会遇到困难。最后,它也会帮助你,因为更多的人可以/将会帮助你是的,我用3个arg打电话给main,但我不太清楚你在做什么。你如何调用/运行你的程序?我想他只会调用它的主方法来运行它。但是,他没有提到。我建议使用英语命名惯例。许多人在阅读此代码时会遇到困难。最后,它也会帮助你,因为更多的人可以/将会帮助你是的,我使用3个参数调用main,结果如下:无法从Gson类型中的json(String,Type)静态引用非静态方法e
addGson 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
使用{…}
作为对象列表,使用[…]作为值列表。使用方括号[]
代替方括号{}
函数是公共输入值(int[]statica int[]inserta,int[]reservea){
设置器是公共void setinsertamount(int[]inserta){this.insertamount=inserta;}
…应该可以正常工作吗?在这一点上,我得到:无法从类型Gson的json(字符串,类型)静态引用非静态方法e
添加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
使用{…}
作为对象列表,使用[…]
作为值列表。使用括号[]
代替大括号{}
该函数是公共输入值(int[]a[]inserta,int[]staticreserva){
setter是public void setinsertamount(int[]inserta){this.insertamount=inserta;}
…应该可以正常工作吗?