Gas*_*lén 0 java arrays int android
我做了一个传递的方法int[],所以在每个位置我想设置4个数字,这就是我做的:
private void loadCars(int[] car){
Json json = new Json(getApplicationContext());
ArrayList<JSONObject> allCars;
allCars = json.gettTodosLosJson();
//Cargas los pictos desde el json
//Seteamos el texto de las opciones
Opcion1.setCustom_Img(json.getIcono(allCars .get(car[0])));
Opcion2.setCustom_Img(json.getIcono(allCars .get(car[1])));
Opcion3.setCustom_Img(json.getIcono(allCars .get(car[2])));
Opcion4.setCustom_Img(json.getIcono(allCars .get(car[3])));
}
Run Code Online (Sandbox Code Playgroud)
所以,当我想设置每辆车的数字时,我这样做:
loadCars(377,643,628,614);
Run Code Online (Sandbox Code Playgroud)
但它不起作用,如果我设置方法就行,(int car1, int car2, int car3, int car4)但我不想这样做.
在方法签名中使用varargs:
private void loadCars(int... car){
//...
}
Run Code Online (Sandbox Code Playgroud)
然后你可以按照你想要的方式调用它:
loadCars(377,643,628,614);
Run Code Online (Sandbox Code Playgroud)
编辑
但你必须考虑@ 0xDEADC0DE的注释:如果你总是需要4个整数,那么最好用它loadCars(int int1, int int2, int int3, int int4)来避免不良结果.
| 归档时间: |
|
| 查看次数: |
77 次 |
| 最近记录: |