在Java中,可以使用循环遍历数组来查找指定的元素。以下是一个简单的示例:
public class Main {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5};
int target = 3;
boolean found = false;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == target) {
found = true;
break;
}
}
if (found) {
System.out.println("Element " + target + " found in the array.");
} else {
System.out.println("Element " + target + " not found in the array.");
}
}
}
这段代码会在数组 arr
中查找元素 target
,如果找到则输出找到的信息,如果没有找到则输出未找到的信息。
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请发送邮件至 55@qq.com 举报,一经查实,本站将立刻删除。转转请注明出处:https://www.szhjjp.com/n/1021579.html