Check whether a value exist in Array List

时间:2018-05-09 02:47:11

标签: java arrays

I want to find steps between 2 inputed texts. For example I have array like:


       String pic2 [] = 20;
       int index=0;
       int i=0;
    String shutterValue [] = {"30", "25", "20", "15", "13", "10", "8", "6";
     for (String val:shutterValue){
                if (val.equalsIgnoreCase(pic2)){
                  index = i;
                }
                i++;
            }
            String Minus4 = shutterValue[index+6];

And input pic20 is "20". So how can I find 4steps between pic2 and value of 4steps after pic2? .

3 个答案:

答案 0 :(得分:0)

您可以将Array转换为ArrayList并找到两个值的索引

List<String> list = Arrays.asList(shutterValue);
int difference = list.indexOf("8") - list.indexOf("20");

答案 1 :(得分:0)

您可以尝试以下代码

import java.lang.reflect.Array;
import java.util.Arrays;

public class Helper {

    public static void main(String[] args) {
        String shutterValue [] = {"30", "25", "20", "15", "13", "10", "8", "6"};

        String no1 = "20";
        int steps = 4;

        int first = Arrays.asList(shutterValue).indexOf(no1); 

        String  value = Array.get(shutterValue,(first + steps)).toString();

        System.out.println(steps + " steps from " + no1 +" is " + value);

    }
}

答案 2 :(得分:0)

对于您的情况,如果您正在使用Arrays,则通过遍历(使用循环)通过Array计算元素的索引位置,然后计算差异。或者您可以转换为ArrayList并使用indexOf获取元素索引并计算2个位置之间的差异。