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)
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:
What does <T> (angle brackets) mean in Java?
(6 answers)
Closed 8 years ago.
So, while looking through others code ive been seeing things like such as in the following code:
List myList = new ArrayList<String>(Arrays.asList(s.split(" ")));
What exactly does this do? I haven't been able to find any documentation, in part by the reason that I don't really know what it is called. And if possible an explanation of what exactly they do?
That particular code generates a list that contains the entries resulted from splitting the s string at each space.
The < String> defines the generic type for the List, and the advantage (among other ones) is that you can call myList.get(index) and not have to cast it to a String.
This is a generic. You are instantiating an ArrayList that stores String objects.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Sorting ArrayList of Objects by Object attribute
Basically I have an ArrayList that stores objects, each of those objects has a field that stores an integer. I want to store the objects in my ArrayList by acending order of that integer.
Is there an easy way to do this, I've looked around the Java API doc but can't find anything that looks suitable, sorry if this is trivial but it sounds like it should be simple enough if I can just find the right documentation.
Thanks
Create your custom comparator class implementing java.util.Comparator and use java.util.Collections.sort(list, comparator).
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.
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.