Home » Archive

Articles tagged with: java ee

Java Codes »

[20 Feb 2009 | No Comment | 64 views]

This example shows how to extend the size of an array. Since arrays are static in size, they cannot be extended in the way collection objects can (for example a Vector).Hence we need to create a new array, add the new data and copy data from the first array.In this example we create an array with three names, then we create another array with the length of 5. We add two names to position 3 and 4 (which is really position 4 and 5 since the first element of an …

Java Codes »

[20 Feb 2009 | No Comment | 66 views]

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 »

[20 Feb 2009 | No Comment | 79 views]

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 …