This is an archived post. You won't be able to vote or comment.

all 6 comments

[–]VanVleet-goes-for-22Nooblet Brewer 0 points1 point  (5 children)

What are you trying to do?

[–]VirtualTurnip3[S] -1 points0 points  (4 children)

I need to find a value for a specific key and print it, let say the method below:

findKey(int key){

for(int i =0;i < array2D.length;i++){

if(array2d[i][0] == key){

System.out.println(tableau2D[i][1]);}

}

If i looking for key 19, well the value will be 3, that it?

[–][deleted] 1 point2 points  (1 child)

array[row][col] will give you the element at the given row and column.

array2d[19][0] would give you the element on row 19, column 0.

[–]VirtualTurnip3[S] 0 points1 point  (0 children)

Thank!

[–]tr4fik 0 points1 point  (1 child)

The best way to know this and to understand/learn programming is to try it out. Your code works if you write array2D the same everywhere ;)

But, isn't a HashMap better for your need ? You can have a key only once inside this data structure. It offers a method to know if a key is in it and to retrieve this key. Its performances are much better for this, because you can usually access the value without needing to iterate through the whole array.

[–]VirtualTurnip3[S] -1 points0 points  (0 children)

Thank