This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In Java, how can I test if an Array contains a certain value?
I have an array setup as follows:
Material[] blockedlevel1 = {
Material.mymaterialone, Material.mymaterialtwo
};
How do I see if a Material is in this array?
How about looking for it in the array?
for (Material m : blockedlevel1) {
if (m.equals(searchedMaterial)) { // assuming that equals() was overriden
// found it! do something with it
break;
}
}
If you want an easy way to check if an element is part of a collection you should probably consider a different data-structure like Set (and use contains()). With Array you can only iterate over the elements and compare each one.
How about looking for it using the Arrays class?
See Arrays#binarySearch
Or as someone suggested, turn your array into a List and use the contains() method. Remember that you may have to override the Material#equals method.
Related
This question already has answers here:
How do I determine whether an array contains a particular value in Java?
(30 answers)
Closed 6 years ago.
I have a similar logic for a method in a Java Class (not the real code, this is simplified for example purposes).
private Boolean method(Boolean booleanValue, SomeObject object) {
return booleanValue ? Arrays.asList(object.getStringsArray()).contains("string") : false;
}
A collaborator who assigned himself to check the PR gave the following comment:
This is inefficient. It is creating a new data structure only to iterate it and check if there is a certain string.
The getStringsArray() method returns a String[], so will using a for-loop be better than Arrays.asList()?
Which way is more efficient to achieve this?
Your co-worker is incorrect when they assert that your method is creating a new data structure.
If you look at the API for Arrays.asList(), it says that it
Returns a fixed-size list backed by the specified array.
There is no reason to write your own code to iterate over the array when you can just wrap a List around it, and use its built-in methods.
This question already has answers here:
How do I determine whether an array contains a particular value in Java?
(30 answers)
Closed 9 years ago.
In Python you can do something like
if 7 in list
return True
Is there anything in java like this? To go "if x in array" without having to do a for loop or several lines of code?
Thanks
Java arrays don't have such properties, but you can either use a collection (preferable a Set, because the lookup methods are the most efficient) or wrap your array with Arrays.asList()
return Arrays.asList(arr).contains(7)
You can use
Arrays.asList(yourArray) - convert array to list
and then
.contains(7) - find value at list
Some other solutions:
http://javarevisited.blogspot.cz/2012/11/4-ways-to-search-object-in-java-array-example.html
The ArrayList class provides method contains(Object).
You need to call Collection contains method to check for existense:
list.contains(7)
If you are already using Commons Lang, there is ArrayUtils#contains.
Convert your array to list using Arrays#asList() and
There is contains() method in List
http://docs.oracle.com/javase/7/docs/api/java/util/ArrayList.html#contains(java.lang.Object)
This question already has answers here:
Java - Removing duplicates in an ArrayList
(19 answers)
Closed 7 years ago.
for (int i = 0; i < arrayP.size(); i++) {
if (arrayP.get(i) == What to put here?) {
arrayP.remove(i)
}
}
If I put i+1 it just compares side by side. How can I get it to run through each element?
Element 1 then run through the whole ArrayList. Then element 2 run through ArrayList.
You can use a LinkedHashSet:
list = new ArrayList<>(new LinkedHashSet<>(list));
If the order of elements does not matter, you should use a normal HashSet instead. In fact, if it's logical to do so, you might consider using a Set instead of a List in the first place. Read about the differences between the two and pick the structure that's more appropriate.
try this
Set arrayPSet = new LinkedHashSet(arrayP);
now arrayPSet holds your array removing it's duplicate values, then you can use it, you can
re-assign your list to this set once again, like
arrayP = new ArrayList(arrayPSet);
now duplicates are removes
just make sure to override the equals() and hashCode() methods if arrayP list holds objects created by you
ok, i updated it maybe this works, i didnt try the order issue, but there are TreeSet/Comparators/Comparables solutions , just try this modification first
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Check whether an array is a subset of another
I have array a = { 1,4,3,5,1,2,3,6 } and array b= { 1,2,3 }.
How can I check if array b elements appear anywhere in array a? Any help will be appreciated. Thanks.
The easiest way is obviously to use the built-in functions. However, Java only has built-in functions to do this for Collection types. If the arrays are not of primitive types, you can do this:
if (Arrays.asList(a).containsAll(Arrays.asList(b))) {
//...
}
If it's a primitive array (int[], etc) this is still the easiest approach, you just need to convert the array into a list manually or using a third-party library. Guava has functions for this:
if ( Ints.asList(a).containsAll(Ints.asList(b)) ) {
//...
}
Use Apache Commons Lang:
for (Object element:b) {
if (ArrayUtils.contains(a,element) == false) return false;
}
return true;
This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Java unmodifiable array
Immutable array in Java
How do I make an array read only so that the elements inside it can only be read but cannot be modified,added or deleted. need to do this in JAVA. Please help. I think merely the use of final keyword wont help.Need to do something more than that at the code level. Thanks in advance!
Short answer is you can't -- final will only guarantee you that the reference to the array itself won't be changed. You can do this with a List though, as the Collections class provides a method for creating a List that cannot be modified (Collections.unmodifiableList) -- that is only if you can change your application to use List rather than array.