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.
Related
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);.
This question already has answers here:
Converting array to list in Java
(24 answers)
Closed 6 years ago.
please see the code below.
int[] intArray={1,2,3,4,3,4,5};
List intList=Arrays.asList(intArray);
System.out.println(intList.contains(1));
Above code is returning false.can any one pls explain why it is so?
Arrays.asList converts your primitive array to a List<int[]> whose single element is the array, intList.contains(intArray) would return true, but intList.contains(1) won't.
If you change your int[] array to Integer[], you'll get your expected output - i.e. a List<Integer> that contains the elements of the original array.
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.
This question already has answers here:
How to split array list into equal parts?
(9 answers)
Split array into two parts without for loop in java
(5 answers)
Closed 8 years ago.
I was wondering if there is a way (simple way hopefully) to take the elements of an array list and put it into 2 other array lists. If the original array list contained ten numbers, is there a way to split the array list in half giving 5 numbers to each new array list? Thanks for your help!
You could use this method:
newArrList = arrayYouWantToSplit.subList(int fromIndex, int toIndex);
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)