Articles tagged with: javacode
Java Codes »
This piece of Java code shows how to convert an array to one of the Colletion types, namely a List.There is a class called Arrays (not to be confused with an array) that has a method called asList() which takes an array as argument and converts it to a List object.Generics can be used to specify the List elements type.
/**
*
* @author www.javabout.com
*/
public class Main {
public void arrayToList() {
String[] cars = {“Dodge”, “Chevrolet”, “BMW”, “Toyota”};
…
Java Codes »
The Arrays class can be used to sort an array of either reference types or primitive types, but the Arrays class does have an overloaded sort method that can sort an array in any way we choose. That overloaded variant of the sort method can only take reference types, and it has an additional parameter which is of type Comparator. This is the parameter that tells how we want the array sorted. In the example below we call the static method reverseOrder on the Collections class. It returns a Comparator …
