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);
Related
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);
This question already has answers here:
Distinction between the capacity of an array list and the size of an array
(7 answers)
Closed 7 years ago.
Why the last println prints out 0 rather than 80?
List<String> list = new ArrayList<String>(80);
System.out.println(list.size());
You have nothing in the ArrayList. ArrayList.size() returns the number of elements in the array, but your array has no elements.
Because you haven't put anything in the collection - just reserved some space for things you will put in the collection.
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.
This question already has answers here:
Sort an array in Java
(19 answers)
Closed 4 years ago.
was wondering if someone could write an example code for elements of array being displayed in alphabetical order.
The elements have come from the users input and have been stored in the array.
Thanks.
A simple example
String[] strArray = {"Carol", "bob", "Alice"};
Arrays.sort(strArray, String.CASE_INSENSITIVE_ORDER);
System.out.println(Arrays.toString(strArray));
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.