checking a string in array [duplicate] - java

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 want to check for a string S whether it is present in string array or not. Is there any direct method to do so ?
EDIT
If the answer excludes the use of List then that would be better.

There are a couple of ways to accomplish this using the Arrays utility class.
If the array is not sorted:
java.util.Arrays.asList(theArray).indexOf(o)
If the array is sorted, you can make use of a binary search for performance:
java.util.Arrays.binarySearch(theArray, o)
See here Where is Java's Array indexOf?

You can convert it to List and check: Arrays.asList(arrayVariable).contains(S)

Related

Is there any method to sort a string array in specific order in java which has alphanumeric elements in it [duplicate]

This question already has answers here:
Sorting arraylist in alphabetical order (case insensitive)
(10 answers)
Closed 11 months ago.
I have an array named "testArray" with elements: {"fdvsd","sd","Edit1648004502584","zxz","automatioion","acas","teg"}
Once I use Arrays.sort(testArray), the elements are sorted as below:
{"Edit1648004502584","acas","automatioion","fdvsd","sd","teg","zxz"}
But I need the array to be sorted as follows:
{"acas","automatioion","Edit1648004502584","fdvsd","sd","teg","zxz"}
Please help me out.
If I understand correctly the issue you are having is the capitalisation of the Strings, when you want it to ignore case. Try the following code:
Arrays.sort(testArray, String::compareToIgnoreCase);

pass array as argument without first element java [duplicate]

This question already has answers here:
How to use subList()
(6 answers)
Closed 3 years ago.
I want to pass an array as argument to a function without first element.
I came up with this solution, but I'm wondering if there are better ways to it.
List<Integer> numbers = new ArrayList<>();
//... in the meantime numbers is an array that contain 1000 elements;
numbers.remove(0);
myFunction(numbers)
You can use subList(firstElement, lastElement); method.
Here please check javadoc.

Search array within another array - Algorithm [duplicate]

This question already has answers here:
Find an array inside another larger array
(15 answers)
Closed 8 years ago.
Is there a way to find an array within another array like
a=[1,2,3,4,5,6,7]
b=[2,3,4]
c=[2,4,5]
// b is child of a, but c is NOT child of a.
Well I know that using Brute-force approach I can find the array within another array. But I want to know that is there any algo that can help me ... or (as I am using JAVA so) is there any built-in feature in JAVA that can help me ?
As already mentioned here :
https://stackoverflow.com/a/3940684/351861:
public static int findArray(Integer[] array, Integer[] subArray)
{
return Collections.indexOfSubList(Arrays.asList(array), Arrays.asList(subArray));
}
Java has builting features for that, apparently.

How to check one arrayList to see if it contains parts of another? [duplicate]

This question already has answers here:
Efficient intersection of two List<String> in Java?
(9 answers)
Closed 10 years ago.
Find what items in the store match the items in the list. Save this list as the best list of items so far
That is the line in the pseudocode that I need to translate into java code.
I have two lists one called itemsNeeded and one called itemsFound. I need to see if the itemsNeeded list contains any items in the itemsFound list. Then I need to save that list. Can anyone demonstrate this?
You could take a brute force approach and do it this way...
Say you have a List to store the matches in, called saveList. I'm also assuming the items in the lists are of type String, but this should work with any Object.
for(String item : itemsNeeded) {
if(itemsFound.contains(item)) {
saveList.add(item);
}
}
You could use: http://commons.apache.org/collections/apidocs/org/apache/commons/collections/CollectionUtils.html
predicatedCollection method
and an EqualPredicate with itemsNeeded list

In Java why is length a method for Strings, while it is a variable for Arrays? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why is String.length() a method?
If I want to know the length of an array I will have to type the following:
array.length
Therefore, it is a variable in which is written the length of the array called 'array'.
On Strings the same thing will look like this:
string.length()
Therefore, it's a method that returns the length of the 'string' variable.
But why did they choose to make those two different, while it basically does the same thing... Is this because of efficiency or why else?
Thank you in advance.
good question.
arrays are fixed in size and therefore have a public final length field.
strings are implementing the CharSequence interface that have the length() function that needs to be implemented.

Categories

Resources