Jun 21

You might think that with turning an array into a List (or the other way around) means that you have to independent collections, but this is not the case. They are now assiciated with each other which means that all data-changes in one collection has an influence to the other one:

String a [] = {“Mailand”, “Berlin”, “Rome”};

List <String> b = Arrays.asList(a);

b.set(2,”Madrid”);
a[0]=”Paris”;

for (int i = 0; i<b.size(); i++)

System.out.print(b.get(i)+” “);

Die Ausgabe lautet:

Paris Berlin Madrid

Since every data-change has an influence of the associated collection an enhancement of List with additional entries is not possible anymore since a fixed size is defined for the Array.

Tagged with:
Jun 18

Maybe you found out already that there is no big difference between Arrays and List – and you are definitely right!

Due to this you can easily turn Lists in Arrays and the other ways around:

String a [] = {“Mailand”, “Berlin”, “Rom”};
List b <String>= Arrays.asList(a);
// changing Array in a List

respectively

Object []c = b.toArray(); // Turning an Array in a List of objects
String [] d = new String[3];
d =b.toArray(d);
// Turning a List in a String-Array, but therefore the List need explicitly declared as a List of Strings, otherwise you will get an CompilerError (Type mismatch)

Tagged with:
May 26

As you can imagine, my previous informations about ArrayList are not enough for SCJP.

More or less I wanted to give you first an overview about all the confusing classes and interfaces before going deeper.

ArrayList is quite often used in a polymorphic  way, eg

List d = new ArrayList(); //syntax at older compilers, but still valid
List <String> e = new ArrayList <String> (); // new syntax from java 5

List a = new ArrayList ();
String b =”Meier”;
a.add(“Peter”);
a.add(b);
a.add(b+b);

for (int i = 0; i < a.size(); i++) { System.out.println(a.get(i)); }

Output is

Peter
Meier
MeierMeier

As already mentioned, ArrayList is similar to Array, but much more powerful. One main advantage ist eg non limitatation  of elements and dynamical expand of ArrayList.

Tagged with:
May 25

It is quite important for SCJP to know exactly differences and similarities of the single classes. Therefore here an overview about the characteristics of the classes:

class abilities
HashMap Connection with a unique key, null keys and multiple null values are allowed 

good performance (dependent on hashCode() implementation, but no order

HashTable Connection with an unique key 

Thread safe

otherwise analogue HashMap

TreeMap Connection with an unique key 

inserting new elements is more time consuming as eg HashMap, but therefore

permanent sorting and

fast, sorted output

LinkedHashMap Connection with an unique key 

faster at adding and deleting elements than HashMap

slower while iterating trough elements

HashSet Uniqueness of values 

fast sorting and finding of elements, but

no order

TreeSet Uniqueness of values 

adding of new elements more time consuming as eg HashSet, therfore

permanent sorting and

fast, sorted output

LinkedHashSet Uniqueness of values, 

high performance at multiple operations

ordered HashSet

ArrayList Values can appear double, 

faster in iterating through elements

Vector Values can appear double 

Thread safe

otherwise analogue ArrayList

LinkedList Values can appear double 

faster in adding and deleting elements than ArrayList

slower in iterating through elements

PriorityQueue “waiting queue” 

sorting by priority

Tagged with:
May 19

There are different kind of Queue, but for SCJP there is only one relevant:

  • PriorityQueue

About Queues:

  • Stack of elements which are taken one by one (typically FIFO – first in, first out)
  • support of all standard Collection Methods
  • additional methods for adding/ removing and to check queue elements
  • A waiting list can take a unlimited amount of elements and returning them back in the order of the insertion. (Similar to supermarkt – those which are first on sales point are served first – first in (there), first out)
  • LinkedList is already expanded with the implementation of queue interface
Tagged with:
May 09

LinkedList has the following abilities:

  • similar to ArrayList with the exception of double linking of elements
  • due to automatical implementation of  java.util.Queue – interface new methods are added, eg
    • poll() // deletion of the oldest (first inserted) value
    • peek () // output of the oldest value
    • offer () // adding values
  • due to the  special build-up especially suitable for  data volumes where  new data is often added or old data deleted
  • is in princible more slowly than ArrayList, adding/ removing is faster

Responsible class is java.util.LinkedList.

Definition/ Implementation is similar to Vector and ArrayList:

LinkedList a;
a = new LinkedList <String>();

LinkedList <Integer> b = new LinkedList ();

LinkedList c = new LinkedList (100); // Attention: CompilerError! This is only possible for Vectors

Using LinkedList is again similar to ArrayList und Vector:
a.add (“Hello”); // adding an object
a.size(); // returning size
a.get(0); // returning first element

Tagged with:
May 08

Vector is similar to ArrayList with one exception: Vector is Thread-safe and ArrayList not.

Responsible class is java.util.Vector.

Definition/ Initialization is similar to ArrayList:

Vector a;
a = new Vector <String>();

Vector <Integer> b = new Vector();

Vector c = new Vector(100); //The size of ArrayList/ Vector can be given but there is no need to do so.

For working with vectors we have the same methods as for ArrayList

a.add (“Hello”); // adding an object
a.size(); // returning size
a.get(0); // returning first element

Tagged with:
May 07

ArrayList have the following characteristics:

  • similar to growing Array
    • elements can easily added/ removed
    • every element has a number/ index starting with 0 for the first element
  • better inserting- and sort-mechanism than arrays
  • growing dynamically – any number of objects can be saved
  • fast iteration
  • fast direct-access
  • ordered (due to array-index)
  • not sorted

In order to work with ArrayLists class java.util.ArrayList needs to be imported.

Here some examples for definition and initialization:

ArrayList <String> a;
a= new ArrayList
<String>();

ArrayList <Integer> b = new ArrayList();

ArrayList c = new ArrayList();


… and a few more examples for using ArrayList:

a.add (“Hello”); // adding an object
a.size(); // returning array-size
a.get(0); // returning the first element  (in case “Hello”)

Tagged with:
preload preload preload
http://www.wikio.de Blog Top Liste - by TopBlogs.de Blogverzeichnis - Blog Verzeichnis bloggerei.de Bloggeramt.de Software
Webbhotell Top Blogs