A collection represents a group of objects, called elements, bound together in different, useful ways. Elements can be added to, removed from or iterated over in a collection. Collections must provide ways to query for state and existence. Here is the set of responsibilities:
Correspondingly, any collection will:
Here's an example of using Collections with a list or array. This take input as a String or String[], turns it into a List< String >, optionally sorts it and optionally creates a String[] or String for output purposes.
(There's a great deal more to collections than what's here. I'll come back and expand it some another time.)
The important bits of what's going on below here are:
List< String > list = new ArrayList< String >(); for( int i = 0; i < this.array.length; i++ ) list.add( this.array[ i ] );
Collections.sort( list );
int arrayLength = list.size(); String[] stringArray = new String[ arrayLength ]; for( int i = 0; i < arrayLength; i++ ) stringArray[ i ] = list.get( i );
StringBuilder sb = new StringBuilder(); for( int i = 0; i < list.size(); i++ ) sb.append( list.get( i ) + " " ); String string = sb.toString();
001.
package
com.etretatlogiciels.examples.collections;
002.
003.
import
java.util.ArrayList;
004.
import
java.util.Collections;
005.
import
java.util.List;
006.
007.
/**
008.
* This class demonstrates some possible somersaults in the life-cycle of a
009.
* String becoming String array becoming sorted Collection.
010.
*
011.
* To stifle perniciousness, the object must start off with a String which
012.
* cannot be changed thereafter: one initial value per one instantiation of
013.
* this class.
014.
*/
015.
public
class
StringArrayToCollection
016.
{
017.
private
String[] array =
null
;
018.
private
List< String > list =
null
;
019.
020.
/**
021.
* Easier start by supplying a simple, space-delimited string such as
022.
* a sentence.
023.
*
024.
* @param toParse - the string to be parsed into an array of String.
025.
*/
026.
public
StringArrayToCollection( String toParse )
027.
{
028.
if
( array ==
null
)
029.
array = toParse.split(
" "
);
030.
makeList();
031.
}
032.
033.
/**
034.
* The basic characteristic of the initial state of an object of this
035.
* class is the String array.
036.
*
037.
* @param array - String[] of any number of elements.
038.
*/
039.
public
StringArrayToCollection( String[] array )
040.
{
041.
if
( array ==
null
)
042.
this
.array = array;
043.
makeList();
044.
}
045.
046.
private
void
makeList()
047.
{
048.
if
(
this
.array ==
null
)
049.
return
;
050.
051.
List< String > list =
new
ArrayList< String >();
052.
053.
for
(
int
i =
0
; i <
this
.array.length; i++ )
054.
list.add(
this
.array[ i ] );
055.
056.
this
.list = list;
057.
}
058.
059.
/**
060.
* Sort the string array. This is destructive in the sense that the
061.
* original order is lost. Note that words beginning with an upper-case
062.
* letter will sort in front since we're not doing anything to neutralize
063.
* case, though we could easily do that by using the version of sort()
064.
* that accepts a Comparator.
065.
*
066.
* @return the sorted string array.
067.
*/
068.
public
List< String > sort()
069.
{
070.
Collections.sort(
this
.list );
071.
072.
return
this
.list;
073.
}
074.
075.
/**
076.
* Turn the string (list) into an array of String--seems to satisfy the
077.
* requirements.
078.
*
079.
* @return the string array.
080.
*/
081.
public
String[] toArray()
082.
{
083.
int
arrayLength =
this
.list.size();
084.
String[] stringArray =
new
String[ arrayLength ];
085.
086.
for
(
int
i =
0
; i < arrayLength; i++ )
087.
stringArray[ i ] =
this
.list.get( i );
088.
089.
return
stringArray;
090.
}
091.
092.
/**
093.
* Return the string (list) to a simple string, easier to display using
094.
* System.out.println(). If sorted, this will be little more than a
095.
* jumble of words.
096.
*/
097.
public
String toString()
098.
{
099.
StringBuilder sb =
new
StringBuilder();
100.
101.
for
(
int
i =
0
; i <
this
.list.size(); i++ )
102.
sb.append(
this
.list.get( i ) +
" "
);
103.
104.
String string = sb.toString();
105.
106.
if
( string.endsWith(
" "
) )
107.
string = string.substring(
0
, string.length() -
1
);
108.
109.
return
string;
110.
}
111.
}