You are on page 1of 5

Java arrays and collections

Java offers two types of constructs where you can store multiple values or objects of the same
type: arrays and collections (for System Dynamics models AnyLogic also offers HyperArray,
also known as "subscripts", a special type of collection for dynamic variables).

Array or collection? Arrays are simple constructs with linear storage of fixed size and therefore
they can only store a given number of elements. Arrays are built into the core of Java language
and the array-related Java syntax is very easy and straightforward, for example the nth element of
the array can be obtained as array[n]. Collections are more sophisticated and flexible. First of all,
they are resizable: you can add any number of elements to a collection. A collection will
automatically handle deletion of an element from any position. There are several types of
collections with different internal storage structure (linear, list, hash set, tree, etc.) and you can
choose a collection type best matching your problem so that your most frequent operations will
be convenient and efficient. Collections are Java classes and syntax for obtaining, e.g., the nth
element of a collection of type ArrayList is collection.get(n).

Java arrays and collections


Please note that indexes in Java arrays and collections start with 0, not with 1! In an array or
collection of size 10 the index of the first element is 0, and the last element has index 9.

Utility class
From Wikipedia, the free encyclopedia
Jump to: navigation, search

In computer programming, a utility class is a class that defines a set of methods that perform
common, often re-used functions. Most utility classes define these common methods under static
(see Static variable) scope. Examples of utility classes include java.util.Collections [1] which
provides several utility methods (such as sorting) on objects that implement a Collection
(java.util.collection [2] ).

Arrays utility class example in java


The java.util.Arrays class contains various utility methods for manipulating arrays such as
sorting and searching. This class also contains a static factory that allows arrays to be viewed as
lists.

The java.util.Arrays class is basically a set of static methods that are all useful for working with
any type of arrays. In addition to that, it has got many utility methods for using with arrays such
as a method for viewing arrays as lists and methods for printing the contents of an array,
whatever be the dimension of the array.

Example of Arrays class

The below example shows you to sort the different type of arrays and convert the array into the
List.

?
1 package com.java.connect.collection;
2
import java.util.Arrays;
3 import java.util.List;
4
5 public class ArraysExample {
6 public static void main(String[] args) {
7 int[] intArray = { 1, 2, 3, 4, 6, 9, 12, 34, 12 };
8 char[] charArray = { 'a', 'o', 'r', 'b', 'u', 'g', 'e' };
String[] stringArray = { "test1", "test2", "test3", "test4",
9 "test5", "test6", "test7", "test8" };
1 // Sort the intArray
0 Arrays.sort(intArray);
11 for (int i = 0; i < intArray.length; i++) {
System.out.println("intArray is after sort : " + intArray[i]);
1
}
2 // Convert the array as List
1 List intList = Arrays.asList(intArray);
3 System.out.println("Size of the int list is : " + intList.size());
1 System.out.println();
// Sort the charArray
4 Arrays.sort(charArray);
1 for (int i = 0; i < charArray.length; i++) {
5 System.out.println("charArray is after sort : " + charArray[i]);
1 }
6 // Convert the array as List
List charList = Arrays.asList(charArray);
1 System.out.println("Size of the char list is : " + charList.size());
7 System.out.println();
1 // Sort the stringArray
8 Arrays.sort(stringArray);
for (int i = 0; i < stringArray.length; i++) {
1 System.out.println("stringArray is after sort : " +
9 stringArray[i]);
2 }
0 // Convert the array as List
2 List stringList = Arrays.asList(stringArray);
System.out.println("Size of the string list is : " +
1 stringList.size());
2 System.out.println();
2 }
2 }
3
2
4
2
5
2
6
2
7
2
8
2
9
3
0
3
1
3
2
3
3
3
4
3
5
3
6
3
7
3
8
3
9

The output of the above code will be as:

?
1
2
3 intArray is after sort : 1
4 intArray is after sort : 2
5 intArray is after sort : 3
6 intArray is after sort : 4
intArray is after sort : 6
7 intArray is after sort : 9
8 intArray is after sort : 12
9 intArray is after sort : 12
10 intArray is after sort : 34
Size of the int list is : 1
11 charArray is after sort : a
12 charArray is after sort : b
13 charArray is after sort : e
14 charArray is after sort : g
15 charArray is after sort : o
charArray is after sort : r
16 charArray is after sort : u
17 Size of the char list is : 1
18 stringArray is after sort : test1
19 stringArray is after sort : test2
stringArray is after sort : test3
20
stringArray is after sort : test4
21 stringArray is after sort : test5
22 stringArray is after sort : test6
23 stringArray is after sort : test7
24 stringArray is after sort : test8
Size of the string list is : 8
25
26
27

Collections utility class example in java


The Collections utility class consists exclusively of static methods that operate on or return
collections. It contains polymorphic algorithms that operate on collections, "wrappers", which
return a new collection backed by a specified collection, and a few other odds and ends.

Some useful method in Collections class

There are some methods those can be used for sorting the collection elements and those are:

Collections.sort(list) Sorts listC. Elements must be Comparable. Stable, O(N log N).

Collections.sort(list, comp) Sorts list using a comparator.

Collections.shuffle(list) Puts the elements of list in random order.

Collections.reverse(list) Reverses the elements of list.

There are some method for searching the element inside collection object and those are:

i = Collections.binarySearch(list, key) Searches list for key. Returns index of element or negative
value if not found. See Binary Search.

i = Collections.binarySearch(list, key, comp) Searches in list for key using Comparator comp.

t = Collections.max(coll) Returns maximum valued Comparable object in collC.

t = Collections.max(coll, comp) Maximum valued object in coll using Comparator comp.

t = Collections.min(coll) Returns minimum valued Comparable object in collC.

t = Collections.min(coll, comp) Minimum valued object in coll using Comparator comp.

There are many additional methods in Collections, but above methods are mostly used.

You might also like