检查多维数组java中是否存在值 [英] Check if value exists in a multidimensional array java

查看:106
本文介绍了检查多维数组java中是否存在值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

没有 for loop ,有没有办法查看多维数组中是否存在值?我找到了

Without a for loop, is there any way to see if a value exists in a multidimensional array? I found

 Arrays.asList(*ArrayName*).contains(*itemToFind*)

但这只会搜索数组的第一维,我需要搜索2维。

but that will only search the first dimension of the array, and I need to search 2 dimensions.

推荐答案

我创建了一个5x5整数数组,初始化为值i * j。
存在方法需要行号和值来搜索。

I created an 5x5 Integer array and intialized with value i*j. Exists method takes a row number and value to search for.

private static Integer[][] myarray = new Integer[5][5];

public static boolean exists(int row, int value) {
    if(row >= myarray.length) return false;
    List<Integer> rowvalues = Arrays.asList(Arrays.asList(myarray).get(row));
    if(rowvalues.contains(value)) return true;
    return exists(row+1, value);
}

这篇关于检查多维数组java中是否存在值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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