public class LinearArray {
private int data[];
private static Random generator = new Random();
public LinearArray(int size) {
data = new int[size];
for (int i = 0; i < size; i++) {
data[i] = 10 + generator.nextInt(91);
}
}
public int lineararray(int searchKey) {
for (int index = 0; index < data.length; index++) {
if (data[index] == searchKey) {
return index;
}
}
return -1;
}
public String toString() {
String temporary = "";
for (int element : data) {
temporary += element + " ";
}
temporary += " ";
return temporary;
}
}
--- Update ---
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int searchInt;
int position;
LinearArray line = new LinearArray(10);
System.out.println(line);
System.out.print("Enter number(-1 to quit): ");
searchInt = input.nextInt();
while (searchInt != -1) {
position = line.lineararray(searchInt);
if (position == -1) {
System.out.println("The integer " + searchInt
+ " was not found.\n");
} else {
System.out.println("The integer " + searchInt
+ " was found in position " + position + ".\n");
}
System.out.print("Enter number(-1 to quit)");
searchInt = input.nextInt();
}
}
}