Displaying inputs in array in alphabetical order - java [duplicate] - java

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));

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.

How to sort an array given a range? [duplicate]

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.

Dividing an array list into two equal parts [duplicate]

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);

Function to shuffle contents of String array in java [duplicate]

This question already has answers here:
How to randomize two ArrayLists in the same fashion?
(9 answers)
Closed 9 years ago.
Is there any built in function to shuffle contents of String array in java/android?
For example,
To shuffle array a.
a[4]={"abc","cde","def","ghi"}
try Collections.shuffle() in java to shuffle the elements inside the array
Collections.shuffle( a);
for (String i : a) {
System.out.println(i);
}

Categories

Resources