查找的Java ArrayList中的值 [英] Java Finding a value in arraylist

查看:128
本文介绍了查找的Java ArrayList中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经加入极少数到数组列表。我会希望找到it.Example一定的价值,我有4,4,9,9,18。我想找到的26值如果列表中的26>最大值将显示18,如果值是17,将显示9,如果值是5,将显示4.又是有实现这个另一种方法搜索因为衬垫的搜索可能是缓慢的。

 搜索值26    [4,4,9,9,18]显示18
    [20,20,29,29,4]显示20
    [28,28,28,1,10]显示28

如果你有这个列表和搜索26时,输出的第一个元素。因为第一元件为< =大于值被搜索

但电流输出是


  

值2的值:9


 公共类的ArrayList {    公共静态的ArrayList<整数GT;一个列表;    公共静态无效的主要(字串[] args){
        ALIST =新的ArrayList<整数GT;();
        aList.add(4);
        aList.add(4);
        aList.add(9);
        aList.add(9);
        aList.add(18);
        int值= 26;
        INT值2 = 0;        的for(int i = 0; I< aList.size();我++){
            如果(aList.get(I)所述; =值){
                如果第(i + 1&下; aList.size()){
                    值2 = aList.get(I)
                }否则如果(ⅰ> aList.size()){
                    值2 = aList.get(I)                }
            }
        }
        的System.out.println(值2的值:+值2);
    }
}


解决方案

我已经写了使用数组的code。您可以轻松将其采用到的ArrayList

  int类型的[] = {} 28,28,28,1,10;
// int类型的[] = {} 20,20,29,29,4; //你的其他输入
// int类型的[] = {4,4,9,9,18};   INT X = 26;   INT liVal = -1;
   的for(int i = 0; I<则为a.length;我++)
       如果(X<一个由[i])//如果我们遇到了一个值> X
       {
          如果(liVal == - 1)//如果我们找不到小于x的最大价值
              liVal =一个由[i]; //返回值> X
          打破;
       }
       否则如果(X>一种由[i])//找到比x的最大值时,
       {
           如果(liVal< A [I])
               liVal =一个由[i];
       }的System.out.println(liVal);

I have added few number into the arraylist . I would want to find the certain value from it.Example i have 4,4,9,9,18. I would like to find the value of 26. If 26 > largest value in the list it will display 18 and if value is 17 it will display 9, and if value is 5 it will display 4. Also is there another method to implement this search because liner search might be slow.

search value 26

    [4,4,9,9,18] display 18
    [20,20,29,29,4] display 20
    [28,28,28,1,10] display 28

if you have this list and search 26, it will output the first element. because the first element is <= than the value being search.

but current output is

Value of value2 : 9

    public class Arraylist {

    public static ArrayList<Integer> aList;

    public static void main(String[] args) {
        aList = new ArrayList<Integer>();
        aList.add(4);
        aList.add(4);
        aList.add(9);
        aList.add(9);
        aList.add(18);
        int value = 26;
        int value2 = 0;

        for (int i = 0; i < aList.size(); i++) {
            if (aList.get(i) <= value) {          
                if (i + 1 < aList.size()) {
                    value2 = aList.get(i);
                } else if(i > aList.size()) {
                    value2 = aList.get(i);

                }
            }
        }
        System.out.println("Value of value2 : " + value2);
    }
}

解决方案

I have written the code using an array. You can easily adopt it to ArrayList

int a[] = {28,28,28,1,10};
// int a[] = {20,20,29,29,4}; // other input of yours
// int a[] = {4,4,9,9,18};  

   int x = 26;

   int liVal = -1;
   for(int i=0; i<a.length;i++)
       if(x < a[i]) // if we met a value > x
       {
          if(liVal==-1) // if we could not find any largest value smaller than x
              liVal = a[i]; // return the value > x
          break;
       }
       else if(x > a[i]) // find the largest value smaller than x, 
       {
           if(liVal < a[i])
               liVal = a[i];
       }

System.out.println(liVal);

这篇关于查找的Java ArrayList中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆