Racing121
Racing121

Reputation: 235

Check whether List<Integer> is equal to an int

This is some of my current code:

Class usedCoords contains:

public List<Integer> USEDX = new ArrayList<Integer>();

main function contains:

if(fX % gridX == 0 && fZ % gridZ == 0 && ALPHA != 0 && usedcoords.USEDX == fX) { }

Note I also done: usedCoords usedcoords = new usedCoords();, that is why I named it usedcoords.

My main task is that I want to make the usedcoords.USEDX == fX possible. Currently I will get an error because fX is an integer. USEDX has integers as well so how do check that the any integer in USEDX is equal to fX?

Thanks in advance.

Upvotes: 0

Views: 382

Answers (3)

Luchostein
Luchostein

Reputation: 2444

For efficiency-sake, you may use Hashtable<Integer> instead of List because it requires O(1) (constant) time to search for a value.

Upvotes: 0

M A
M A

Reputation: 72884

Use List#contains() -- and it's more readable and conventional not to have variable names start with an uppercase unless they are constants:

if (fX % gridX == 0 && fZ % gridZ == 0 && alpha != 0 && usedcoords.usedX.contains(fX)) {
   ...
}

The int variable fX will be automatically boxed into the Integer type by the compiler.

Upvotes: 3

Elliott Frisch
Elliott Frisch

Reputation: 201527

USEDX has integers as well so how do check that the any integer in USEDX is equal to fX?

By calling List.contains(Object) which returns true if this list contains the specified element. Something like,

if (USEDX.contains(fx)) {
    // ...
}

Upvotes: 3

Related Questions