简体   繁体   English

如何在arraylist中找到特定值?

[英]how can I find a specific value in a arraylist?

I have a list of a list that I'm being passed over as data. 我有一个列表,该列表将作为数据传递给我。 The example they gave was the object will look like this: 他们给出的对象实例如下所示:

List assetClassCorrelationMatrix = new ArrayList();
List m1 = new ArrayList();
m1.add(2);
m1.add(4);
m1.add(0.8979);
assetClassCorrelationMatrix.add(m1);

EDIT: OK, so I'm sorry I was not clear. 编辑:好的,所以对不起我不清楚。 I was thinking that this was going to be in the structure of 我当时以为这将是

correlation[2][4] = 0.8979

Instead they are giving me: 相反,他们给了我:

correlation[0] = 2;
correlation[1] = 4;
correlation[2] = 0.8979;

I just need to get back whatever is in correlation[2] when I just have the values of what is in correlation[0] and correlation[1] 当我只具有correlation[0]correlation[1]的值时,我只需要取回correlation[2]任何值

Help? 救命?

You don't need other values for finding a specific value in nested arraylist. 您不需要其他值即可在嵌套arraylist中查找特定值。 If you are looking for a specific value in arraylist assetClassCorrelationMatrix , you can do this way: 如果要在arraylist assetClassCorrelationMatrix中查找特定值,则可以执行以下方法:

double num = 0.8979;        
for (Object object: assetClassCorrelationMatrix) {            
    List list = (List) object;                                    
    if(list.contains(num))
        System.out.println("The number " + num + " is found at index =  " + list.indexOf(num));
}

Edit: After you edited question, it seems radical change of context. 编辑:编辑问题后,上下文似乎发生了根本变化。 Now you have two value (like index value 2 and 4 in 2D array) and you want to get the value of the index. 现在,您有两个值(例如2D数组中的索引值2和4),并且想要获取索引的值。 You could do this using 2D array easily. 您可以轻松地使用2D阵列执行此操作。 But if you want to stick with list, you could go this way: 但是,如果您要坚持使用列表,则可以采用以下方式:

    List assetClassCorrelationMatrix = new ArrayList();

    int a=2, b=4;

    List m1 = new ArrayList();
    List m2 = new ArrayList();        

    // inserting values
    m2.add(0, 1.1); 
    m2.add(1, 2.0);
    m2.add(2, 0.5);
    m2.add(3, 0.8979);

    assetClassCorrelationMatrix.add(m1);
    assetClassCorrelationMatrix.add(m2);

    List list = (List) assetClassCorrelationMatrix.get(a-1);
    Number number = (Number) list.get(b-1);
    System.out.println("List = " + a + " and index = " + b + " and the number = " + number);

Start by specifying properly the type of your lists, it should be something like this: 首先正确指定列表的类型,应该是这样的:

List<List<Number>> assetClassCorrelationMatrix = new ArrayList<>();
List<Number> m1 = new ArrayList<>();

Then you can access to your value 0.8979 using List#get(int) , like this: 然后,您可以使用List#get(int)来访问值0.8979 ,如下所示:

Number n = m1.get(2); // The indexes start from 0, so the index of the 3th element is 2

If you want to access it from assetClassCorrelationMatrix , it would be: 如果要从assetClassCorrelationMatrix访问它, assetClassCorrelationMatrix

Number n = assetClassCorrelationMatrix.get(0).get(2);

You can identify this value among the other by checking the type, indeed 2 and 4 will be converted automatically as Integer due to auto-boxing and 0.8979 as Double . 您可以通过检查类型来识别此值,实际上24将由于自动装箱而自动转换为Integer0.8979将转换为Double

for (Number n : m1) {
    if (n instanceof Double) {
        // Here is the code that treats the Double value
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM