Jun 30

I already introduced some methods of set bevore:

a.add(11);
a.size();
a.get(0); // CompilerError; working only for List, not for Set
a.remove(11); // deletion of element “11″ (no index!)

Today I would like to go a little bit deeper; let’s have a look on the following example:

System.out.print(a.add(“Mailand”));
System.out.println(” “+a.add(a));
System.out.println(
” “+a.add(new Employee()));
System.out.println(
” “+a.add(“Mailand”));

In case we have defined Set a = new HashSet(); bevore, we get the following output:

true true true false

In case we have definded Set a = new TreeSet(); bevore, we get an CompilerError since TreeSet wants to save the values in an sorted way, which is due to the different objects not possible.

Tagged with:
Jun 27

There are 2 different possibilities to declare an iterator:

Iterator <String> iterator_1 = a.iterator();
Iterator iterator_2 = a.iterator();

On both iterator the functions next() and hasNext() can be used in the same way, but there are also some differences :

while (iterator_1.hasNext()){

String c = iterator_1.next(); // Since iterator is explicitely defined as a String it’s value can be assigned to a String-Object without problems
String d = (String)iterator_2.next(); //Since iterator is defined without any explizit type it is automatically from type Object. Therefore a Cast is neccessary for giving its value to a String Object.

}

Tagged with:
Jun 24

An iterator is an utility which is independent from data and object structure. It is used to iterate through the single elements of the collection. Additionally it offers some functions to check and return elements. It can be compared with some kind of data stream since the direction is always the same – it can neither going forward nor backward. In case an object should be viewed or handled twice it needs either to be stored in an additional variable or the iterator need to initiated once again.

The iterator is most useful for List, but can also be used for other kinds of  collections (eg Set):

List <String> a = new ArrayList <String>();
a.add(“Mailand”);
a.add(“Berlin”);
a.add(“Rom”);

Iterator <String> iterator_a = a.iterator(); // creation of an iterator which can iterate over the single elements.

while (iterator_a.hasNext())
// as long as the iterator has more elements

System.out.print(iterator_a.next()+” “); // print out the next element

 

The Output is

Mailand Berlin Rom

Tagged with:
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:
Jun 15

Let’s increase complexity and bring Comparator in the game :)

import java.util.*;

class NameSort implements Comparator <String>{

public int compare(String a ,String b){ return (b.compareTo(a)); }

}

 

class Employee{

public static void main(String[] args) {

String a [] = {“Mailand”, “Berlin”, “Rom”};
NameSort ns = new NameSort();
Arrays.sort(a,ns); // sorting with Comparator
System.out.println(Arrays.binarySearch(a,”Paris”, ns)); // search analogue to sorting with Comparator

}

}

It’s quite important to work always in the same way – means to use Comparator in sorting and search or not to sue Comparator at all. Please keep also in mind that Comparator is not working with primitive data types.

Tagged with:
Jun 12

We know now how to use method binarySearch(Obj,Suchstring) and the result of an successful search, but what happens in case the search is not successful as in our next example:

import java.util.*;
class Employee{

public static void main(String[] args) {

String a [] = {“London”, “Berlin”, “Rome”};
Arrays.sort(a);
System.out.println(Arrays.binarySearch(a,”Cologne”));

ArrayList b = new ArrayList();
b.add(“London”);
b.add(“Berlin”);
b.add(“Rome”);
Collections.sort(b);
System.out.println(Collections.binarySearch(b, “Cologne”));

}

}

The Output is here 2 times -2; quite confusing, isn’t it?

The ‘-’sign shows that nothing is found. Since the value 0 would be already returned in case of an successful search (in case it is on first position and has therefore index 0) it is counted down from -1 (for the possible insertion on first place).

Tagged with:
Jun 09

Arrays and C0llections are equal in one point – you can use method binarySearch(Obj,search-string) either from Class Array or Collections in order to search for values; but be careful: before searching you should sort the values; otherwise the result is not predictable.

Here on example:

import java.util.*;

class Employee{

public static void main(String[] args) {

String a [] = {“London”, “Berlin”, “Rome”};
Arrays.sort(a);
System.out.println(Arrays.binarySearch(a,”Rome”));

ArrayList b = new ArrayList();
b.add(“London”);
b.add(“Berlin”);
b.add(“Rome”);
Collections.sort(b);
System.out.println(Collections.binarySearch(b, “Rome”));

}

}

The Output is in both cases 2; since the index for Rome is 2.

Tagged with:
Jun 06

Please keep in mind that even the powerfulness sorting-mechanism have their constraints – in case the values are not comparable with each other; eg in case completely different objects are stored in one collections (instance of Integer(); Employee(), and so on) they are not comparable with each other and therefore not sortable.

Tagged with:
Jun 04

Typically in SCJP you need to know differences and similarities:

Comparable Comparator
interface java.lang.Comparable interface java.util.Comparator
Change of classes which instances should be sorted typically creation of a separate class (or at least a separate instance for comparison) of the sorting class
there can be only one sorting order due to different classes (or instances) many different sortings can be created
necessary implementation:
public int compareTo (Obj1)
necessaryimplementation:
public int compare (Obj1, Obj2)
preload preload preload
http://www.wikio.de Blog Top Liste - by TopBlogs.de Blogverzeichnis - Blog Verzeichnis bloggerei.de Bloggeramt.de Software
Webbhotell Top Blogs