[英]Can't set numbers on int[]
我已經制作了一個傳遞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])));
}
因此,當我想為每輛車設置編號時,請執行以下操作:
loadCars(377,643,628,614);
但它不起作用,如果我將方法設置為(int car1, int car2, int car3, int car4)
但我不想那樣做。
在方法簽名中使用varargs :
private void loadCars(int... car){
//...
}
然后您可以按照自己的方式進行調用:
loadCars(377,643,628,614);
編輯
但是,您必須考慮@ 0xDEADC0DE的注釋:如果始終需要4個整數,則最好使用loadCars(int int1, int int2, int int3, int int4)
以避免產生不良結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.