For-each Loop

For-each Loop atau kadang ada yang menyebut for in adalah fasilitas looping yang ada pada java sejak java 5 (java tiger) dirilis penggunaannya hampir sama dengan for biasa tetapi for each lebih di khususkan/lebih terasa ampuh untuk tipe – tipe data collection

sintaks nya

for (type var : coll) {
    body-of-loop
}

perbandingan penggunaan dengan for lama
kita biasa menggunakan

//... For loop using index.
for (int i = 0; i < arr.length; i++) { 
    type var = arr[i];
    body-of-loop
}

jika diganti dengan for each

//... For each loop over all elements in arr.
for (type var : arr) {
    body-of-loop
}
[/sourcecode ]

contoh lain

//... Loop using explicit iterator.
for (Iterator iter = coll.iterator(); iter.hasNext(); ) {
    type var = iter.next();
    body-of-loop
}
[/sourcecode ]
jika kita gunakan for each

//... Foreach loop over all elements in arr.
for (type var : coll) {
    body-of-loop
}

ok langsung ke real code

public class ArrayForLoop {
    
    /** Creates a new instance of ArrayForLoop */
    public ArrayForLoop() {
    }
    
    public static void main(String[] args) {
        int[] intArray =  new int[] {0,1,2,3,4,5};
        
        for (int i : intArray) {
            System.out.println(i);
        }
    }
}

hasil

Hasil penggnaan For-Each

tips penggunaan for each
for each digunakan pada situasi berikut ini

String Item

This code use to make StringItem in j2me
Kode dibawah ini digunakan untuk membuat StringItem di j2me

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.midlet.MIDlet;
/**
* free to distribute but forbiden to delete this statement
* @author b1ms.wordpress.com
* ym : thats_myboy@yahoo.com
* “share code for free”
*/
public class CreatingStringItem extends MIDlet  {
private Display display;

private Form form;
private StringItem str1;
private StringItem str2;
public CreatingStringItem() {
display = Display.getDisplay(this);

form = new Form(“Display Form “);
str1 = new StringItem(“StringItem 1 “, “test stringitem 1”);
str2 = new StringItem(“StringItem 2: “, “test stringitem 2”);

form.append(str1);
form.append(str2);
}

public void startApp() {
display.setCurrent(form);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

}

download source

Running program

smoga bermanfaat
hope useful for u

Ditulis dalam dasar, j2me. Tag: , . 1 Comment »

List

This code use to make List in j2me
Kode dibawah ini digunakan untuk membuat List di j2me

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.*;

/**
* free to distribute but forbiden to delete this statement
* @author b1ms.wordpress.com
* ym : thats_myboy@yahoo.com
* “share code for free”
*/
public class CreatingList extends MIDlet {
private Display display;
private List list;

public CreatingList() {
display = Display.getDisplay(this);
list = new List(“Select one”, List.EXCLUSIVE);
list.append(“isi 1”, null);
list.append(“isi 2”, null);

}

public void startApp() {
display.setCurrent(list);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

}

download source

Running program

Untuk bentuk list yang lain
For the different list

list = new List(“Select one”, List.EXCLUSIVE);

Dapat diganti
Can change with

list = new List(“Select one”, List.IMPLICIT);

atau
or

list = new List(“Select one”, List.MULTIPLE);

smoga bermanfaat

hope useful for u

Ditulis dalam dasar, j2me. Tag: , . Leave a Comment »

Form

This code use to make form in j2me
Kode dibawah ini digunakan untuk membuat form di j2me

import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
import javax.microedition.midlet.MIDlet;

public class CreatingForm extends MIDlet  {
private Display display;

private Form form;

public CreatingForm() {
display = Display.getDisplay(this);

form = new Form(“Display Form “);
}

public void startApp() {
display.setCurrent(form);
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}

}

download source

Running program

Ditulis dalam dasar, j2me. Leave a Comment »