代碼
提交代碼
public class VariableParameter1 {
public void search(int element, int... elements) {
boolean existed = false;
for (int e: elements) {
if (e == element) {
existed = true;
break;
}
}
if (existed) {
System.out.println("找到元素:" + element);
} else {
System.out.println("未找到元素:" + element);
}
}
public static void main(String[] args) {
// 創(chuàng)建對象
VariableParameter1 obj = new VariableParameter1();
// 調(diào)用方法
obj.search(2, 1,2,3,4);
// 定義數(shù)組參數(shù)
int[] arr = {1,2,3,4};
// 將數(shù)組傳遞給可變參數(shù)列表
obj.search(2, arr);
}
}
運行結(jié)果