📄 linearsearch.java
字号:
import javax.swing.JOptionPane;
public class LinearSearch {
static final int LISTLENGTH = 10;
public static void main(String[] args) {
int[] list = new int[LISTLENGTH];
String output = "";
output += "这个数列是:";
for(int i = 0; i < list.length; i++) {
list[i] = (int)(Math.random() * 100);
output += list[i] + " ";
}
JOptionPane.showMessageDialog(null,output,"例子(数 列)",JOptionPane.INFORMATION_MESSAGE);
String keyString = JOptionPane.showInputDialog(null,"输入键值:","输 入",JOptionPane.QUESTION_MESSAGE);
int key = Integer.parseInt(keyString);
int index = linearSearch(key,list);
output = "";
if(index != -1)
output = "找到,其下标是:" + index;
else
output = "键值是:" + key + "在列表中没有找到";
JOptionPane.showMessageDialog(null,output,"输出示 例",JOptionPane.INFORMATION_MESSAGE);
}
//LinearSearch method
public static int linearSearch(int key,int[] list) {
for(int i = 0; i < list.length; i++) {
if(key == list[i])
return i;
}
return -1;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -