This question already has answers here:
How to sort an ArrayList<String> based on specific index range
(2 answers)
Closed 5 years ago.
First of all I am using java and my first objective is to use bubble sort to sort an array in descending order given the range lowindex..highindex (inclusive). So my question is how to sort an array given a range? An example would be greatly appreciated to get me started on doing this.
That wheel has already been invented:
Arrays.sort(array, lowIndex, highIndex + 1);
See doc.
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:
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:
Remove multiple elements from ArrayList
(16 answers)
Closed 7 years ago.
I have an array of indices at which elements must be removed from a List, e.g.: (4), (7), (8).
The problem is:
1) Can't use a For-loop, the size changes after each delete (ArrayList.remove(i))
2) Can't use an Iterator with an updated counter, the counter will also not work anymore (Iterator.remove()).
Found a nice solution here: Remove multiple elements from ArrayList
Collections.sort(indices, Collections.reverseOrder());
for (int i : indices)
strs.remove(i);
That seems to be the only fast and simple solution. Reverse-sort first, then go through the indices and remove.
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);