//顺序查找,线性查找 //int[] arr1 = new int[]{34,54,3,2,65,7,34,5,76,34,67}; //int target = 5; //方式1 boolean flag = true; for (int i = 0; i < arr1.length; i++) { if(arr1[i] == target) { System.out.println("找到了在"+i+"位置"); flag = false; break; } } if(flag == true) { System.out.println("没有找到"); } //方式2 int i = 0; for (; i < arr1.length; i++) { if(arr1[i] == target) { System.out.println("找到了在"+i+"位置"); break; } } if(i == arr1.length) { System.out.println("没有找到"); }
//顺序查找,线性查找
//int[] arr1 = new int[]{34,54,3,2,65,7,34,5,76,34,67};
//int target = 5;
//方式1boolean flag = true;
for (int i = 0; i < arr1.length; i++)
{
if(arr1[i] == target)
{
System.out.println("找到了在"+i+"位置");
flag = false;
break;
}
}
if(flag == true)
{
System.out.println("没有找到");
}//方式2
int i = 0;
for (; i < arr1.length; i++)
{
if(arr1[i] == target)
{
System.out.println("找到了在"+i+"位置");
break;
}
}
if(i == arr1.length)
{
System.out.println("没有找到");
}
JAVA数据结构之顺序表线性表的顺序查找,线性查找的两种方式------JAVA
于 2023-04-11 20:12:31 首次发布