Search array within another array - Algorithm [duplicate] - java

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.

Related

How to add an item to start of an array in Java [duplicate]

This question already has answers here:
Simplest way to add an item to beginning of an array in Java
(9 answers)
Closed 9 months ago.
I am new to Java and I have an array
int [] A = {140,150,160,170,180,190}
I need to add infinity at the beginning of the array
like so:
int [] A = {inf,140,150,160,170,180,190}
I know from Python to do it like so:
A=[140,150,160,170,180,190]
A=[float('inf')]+A
I need a way to do that in Java as simple as Python without loops
It's important to say I need the array / list(A=[140,150,160,170,180,190]) to be given with the code.
You can use Java ArrayList that implements List interface and has a method add(int,E)
So, you can use something like list.add(0, myObj);.

Converting Object[] to List<Object> in this one-liner function in Java [duplicate]

This question already has answers here:
Converting Array to List
(9 answers)
Retrieving a List from a java.util.stream.Stream in Java 8
(15 answers)
Closed 3 years ago.
still very new to Java, my apologies if this has appeared before.
Basically here is the original code
public static MarketType[] convert(final String[] values) {
return ofNullable(values).map(v -> Stream.of(values))
.orElse(Stream.empty())
.map(v -> getMarketType(v))
.toArray(MarketType[]::new);
}
Since other functions changed, I really need the return type to be List<MarketType> instead of MarketType[], but is there any way that can achieve this with the minimal amount of modification for the original code?
I have been trying to put different things in the toArray function but nothing really worked.
Any help appreciated!

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.

Trying to create an array in Java. What did I just do? [duplicate]

This question already has answers here:
What's the simplest way to print a Java array?
(37 answers)
Closed 8 years ago.
I'm new to Java but not new to programming. I ran the following code in Eclipse:
int[] intArray = new int[5];
System.out.println(intArray);
and received the following output:
[I#17f7be7b
I'm sure StackOverflow already contains the correct way to create an array in Java. What I'd like to know is: what did I do?
Edit: Sorry... The link above my post isn't a duplicate question, and it doesn't answer my question. TylerAndFriends' link is closer, but I was hoping for an explanation of exactly what I printed. Tyler's linked thread says "the default method is to display the object's class name representation, then "#" followed by its hashcode". Can someone elaborate?
Use this
for (int i : intArray) {
System.out.println(i);
}

checking a string in array [duplicate]

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)

Categories

Resources