The Source for Java Technology Collaboration
User: Password:
Register | Login help    

Search

Online Books:
java.net on MarkMail:


Collection: An abstract dynamic array

Posted by alois on November 11, 2005 at 2:31 AM PST

As a matter of introduction to my weblog, a little article about some experience I made some years ago when was back to java after some years of development with others languages. With my brand new development skills; I searched the best way to manage dynamic array.

Array is used in variety of process (and language) and is best way to manage large amount of data in computer memory.

Java language provide multiple dynamic array type (also know as list or collection), making beginner confusing and perhaps doing some mistakes, that isn't problems at beginning but can become an headache later... in an other way this variety of dynamic array type provide to developers more flexibility and power to do some specific task (sorting, enumerating, searching).

To illustrate the concept of collection, I will use a simple method that print line per line the content of each element of a dynamic array.

The mistake I do when starting using dynamic array is defining a fixed dynamic array class type, like that:

public void exemple(){
	ArrayList list=new ArrayList();
	list.add("Java");
	list.add("Is")
	list.add("POWERFUL");
	print(list);
}
public void print(ArrayList list){
	int rank
	String string;
	for(rank=0;rank!=list.size();rank++){
		string=(String)list.get(rank);
		System.out.println(string);
	}
}

This working fine until the day I needed to use others dynamic array list (to enable extended feature) but don't want to code another method to manage it.

The first things I thought to do is to convert all Array List to the new dynamic array type, but it's stupid because other list don't need all specific features.

After making a reflection about OOP and java architecture, I thought about doing my own dynamic interface to resolve this problem... And when searching in Java API, I found java.util.Collection interface.

I think all beginner need to start to learn dynamic array in java with Collection.

Collection isn't a class, it's an interface. This OOP powerful feature enabling developer to create methods that abstract the concept of dynamic array and then do some basic task doesn't matter which kin of dynamic array is.
Developer can create customized dynamic array class implementing collection, enabling compatibility with all collection features.

So let's look on the example code after making use of collection interface:

public void exemple(){
	Collection list=new ArrayList();
	list.add("Java");
	list.add("Is")
	list.add("POWERFUL");
	print(list);
}
public void print(Collection list){
	Iterator iterator=list.iterator();
	String string;
	while(iterator.hasNext()){
		string=(String)iterator.next();
		System.out.println(string);
	}
}

Java interface is a powerful OOP feature, and sometimes forget by developers (doing complicated stuff instead of creating a simple interface).

When creating extended class it's always good things to think about an interface usability.

Related Topics >> Programming      
Comments
Comments are listed in date ascending order (oldest first)