我尝试使用Arrays.toString(array));
打印我的数组,但它仍然给了我错误...而且ELEMENT IS FOUND也是错误的声明但当我试图搜索数组中的元素时它与真实的语句混合,
例如......
我在我的阵列中搜索了4:4,2,3,5
但是ELEMENT IS FOUND仍在展示。
import java.util.Scanner;
public class linee {
public static void main(String[] args) {
int String;
int value;
Scanner in = new Scanner(System.in);
System.out.print("Input the Number of Element: ");
String n = in.nextLine();
int num = Integer.parseInt(n);
String array[] = new String[num];
for (int i = 0; i < array.length; i++) {
System.out.print("Input the Number at array index " + i + ": ");
array[i] = in.nextLine();
}
Linear(array);
System.out.print("\nDo you want to continue? YES = 1, NO = 2: ");
value = in.nextInt();
if (value == 1) {
main(args);
} else if (value == 2) {
System.out.println("\nThank you for using the program.");
}
}
public static void Linear(String[] array) {
boolean flag = false;
String key = "";
int index = 0;
Scanner in = new Scanner(System.in);
System.out.print("Enter the number that you want to search: ");
key = in.nextLine();
for (int i = 0; i < array.length; i++) {
if (array[i].equals(key)) {
flag = true;
index = i;
}
}
if (flag == true) {
System.out.println("Elements: " + Arrays.toString(array));
System.out.println("ELEMENT IS FOUND AT INDEX " + index);
} else {
System.out.println("ELEMENT IS NOT FOUND");
}
}
}
答案 0 :(得分:0)
来自Object.toString(Object[] a)
规范:
返回指定内容的字符串表示形式 阵列。如果数组包含其他数组作为元素,则它们是 由继承自的Object.toString()方法转换为字符串 对象,描述其身份而非其内容。
也就是说,您可能希望以其他方式打印元素。一个选项是:
System.out.println("Elements: ");
for(String str: array) {
System.out.println(str);
}