鱼C论坛

 找回密码
 立即注册
查看: 3405|回复: 5

[已解决]数组查询

[复制链接]
发表于 2020-5-7 17:07:15 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
查询数组中的元素在数组中第几个位置?
这道题怎么做?
最佳答案
2020-5-7 17:57:28
  1. package day02;

  2. public class Array_Demo {
  3.     //查找指定元素,第一次出现在数组中的索引位置
  4.     public static void main(String[] args) {
  5.         int[] arr = {11, 22, 5, 44, 55};
  6.         int Index=getIndex(arr,12);//调用方法
  7.         System.out.println(Index);//输出返回值
  8.     }

  9.     public static int getIndex(int[] arr, int value) {
  10.         for (int i = 0; i < arr.length; i++) {
  11.             if (arr[i] == value) {
  12.                 return i;
  13.             }
  14.         }
  15.         return -1;//如果未找到返回-1
  16.     }
  17. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-5-7 17:57:28 | 显示全部楼层    本楼为最佳答案   
  1. package day02;

  2. public class Array_Demo {
  3.     //查找指定元素,第一次出现在数组中的索引位置
  4.     public static void main(String[] args) {
  5.         int[] arr = {11, 22, 5, 44, 55};
  6.         int Index=getIndex(arr,12);//调用方法
  7.         System.out.println(Index);//输出返回值
  8.     }

  9.     public static int getIndex(int[] arr, int value) {
  10.         for (int i = 0; i < arr.length; i++) {
  11.             if (arr[i] == value) {
  12.                 return i;
  13.             }
  14.         }
  15.         return -1;//如果未找到返回-1
  16.     }
  17. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-8 16:00:00 | 显示全部楼层
    int count = 0;
    int num[]; // num is the array we wanna search . target is the int we wanna search in array.
    while(num[count] != target){
        count++;
    }
   
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-8 17:26:52 | 显示全部楼层
题在哪
没看到
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-9 13:08:53 | 显示全部楼层
  1. Scanner in = new Scanner(System.in);
  2.         int[] a = {1,2,3,4,5};
  3.         int b = in.nextInt();
  4.         boolean c = false;
  5.         for(int i=0; i<a.length; i++)
  6.         {
  7.                 if(b == a[i])
  8.                 {
  9.                         System.out.println("b在a的第"+(i+1)+"个位置上");
  10.                         c = true;
  11.                         break;
  12.                 }
  13.         }
  14.         if(c == false)
  15.              {
  16.                      System.out.println("b不在a中");
  17.              }
复制代码

就是这种格式
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-9 22:04:59 | 显示全部楼层
  1. public int IndexArr(int[] arr, int val){

  2.         for(int i=0; i<arr.length; i++){
  3.                 if(arr[i] == val){
  4.                         return i;
  5.                 }
  6.         }

  7.         return -1;

  8. }
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-18 17:35

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表