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

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

Related

how i can convert from int[] to List<Integer> in java [duplicate]

This question already has answers here:
Converting array to list in Java
(24 answers)
Closed 1 year ago.
I have an array as the integer I want to convert her to List without the use of loops
int[] arrya={1,2,3,4,5};
//List<Integer> myList=Arrays.asList(arrya);
You can make use of streams
int[] arrya={1,2,3,4,5};
List<Integer> list = Arrays.stream(arrya).boxed().collect(Collectors.toList());

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.

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

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

why List.contain is returning false [duplicate]

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.

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

Categories

Resources