我有一个char变量。我想找出2D char数组中与我的char变量具有相同元素的元素,如果有一个元素,我想获取该元素的索引号
您可以创建一个包含两个索引的类。您的方法将返回此类的实例。
char array[][] = new char[10][10];
class Tuple //helper class
{
public int i, j;
Tuple(int i, int j)
{
this.i = i;
this.j = j;
}
}
public Tuple matches(char c) //return indices (Tuple-instance) or null if not found
{
for(int i = 0; i < array.length; i++)
{
for(int j = 0; j < array[0].length; j++)
{
if(array[i][j] == c)
{
return new Tuple(i, j);
}
}
}
return null;
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句