A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 呵呵君 中级黑马   /  2015-6-26 23:08  /  268 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 呵呵君 于 2015-6-26 23:27 编辑
  1. <div class="blockcode"><blockquote>  public static void main(String[] args)
  2.         {
  3.               int arr[]={28,32,2,321,12,3,123,2,123,23,12};
  4.                         int search=getKey(arr,2);
  5.                         System.out.println(search);
  6.         }
  7.         public static int getKey(int []arr,int key)
  8.         {
  9.                         for (int x=0;x<arr.length ; x++)
  10.                         {
  11.                                 if (arr[x]==key)
  12.                                 {
  13.                                         return x;
  14.                                 }
  15.                         }
  16.                     return -1;
  17.         }
  18.         
  19. }
复制代码


[/code]小弟今天练习时无意间敲出了一个含有相同元素的数组,用毕姥爷的方法无法查出第二个元素的索引.想知道在这个方法的基础上,如何查找出数组中所用相同元素的索引.不使用直接输出的情况下,还有其他方法吗?
ps:第一次发帖,语言有点乱.望见谅!

评分

参与人数 1黑马币 +6 收起 理由
牧师1990 + 6 赞一个!

查看全部评分

2 个回复

倒序浏览
你可以使用一个数组返回所有的元素的索引
  1.         public static void main(String[] args) {
  2.                 int arr[] = { 28, 32, 2, 321, 12, 3, 123, 2, 123, 23, 12 };
  3.                 int search[] = getKey(arr,2);
  4.                 for (int i = 0; i < search.length; i++) {
  5.                         //如果不为-1则为索引值输出
  6.                         if(search[i]!=-1){
  7.                                 System.out.print(search[i]+" ");                               
  8.                         }
  9.                 }
  10.         }

  11.         public static int[] getKey(int[] arr, int key) {
  12.                 //记录索引值的位置
  13.                 int temp[] = new int [arr.length];
  14.                 //初始化所有的索引值为-1
  15.                 for (int i = 0; i < temp.length; i++) {
  16.                         temp[i] = -1;
  17.                 }
  18.                 //临时变量记录数组目前的位置
  19.                 int flag = 0;
  20.                 for (int x = 0; x < arr.length; x++) {
  21.                         if (arr[x] == key) {
  22.                                 //如果找到索引,存入数组
  23.                                 temp[flag++] = x;
  24.                         }
  25.                 }
  26.                 return temp;
  27.         }
复制代码

如果学习到集合的话,可以返回一个List集合
  1.         public static void main(String[] args) {
  2.                 int arr[] = { 28, 32, 2, 321, 12, 3, 123, 2, 123, 23, 12 };
  3.                 List<Integer> list = getKey(arr, 2);
  4.                 for (Integer integer : list) {
  5.                         //所有的索引值
  6.                         System.out.print(integer+" ");
  7.                 }
  8.         }

  9.         public static List<Integer> getKey(int[] arr, int key) {
  10.                 //记录索引值的位置
  11.                 List<Integer> list = new ArrayList<Integer>();
  12.                 for (int x = 0; x < arr.length; x++) {
  13.                         if (arr[x] == key) {
  14.                                 //如果找到索引,存入数组
  15.                                 list.add(x);
  16.                         }
  17.                 }
  18.                 return list;
  19.         }
复制代码




回复 使用道具 举报
qxc1281 发表于 2015-6-27 08:35
你可以使用一个数组返回所有的元素的索引
如果学习到集合的话,可以返回一个List集合

十分感谢
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马