Monday, August 11, 2008

Array dan Vector di JAVA

1)Kapasitas komponen array adalah n-1, kalau kapasitas vector tidak usah diinisialisasi tdk apa2
Note :
“capacity” di vector adalah jumlah slot yang tersedia
“size” di vector adalah jumlah slot yang sudah terisi data
=================
Program Array :
=================
class Array1
{

public static void main ( String[] args)
{
Integer[] dataku = new Integer[5];
dataku[0] = 10 ;
dataku[1] = 11 ;
dataku[2] = 12 ;
dataku[3] = 13 ;
dataku[4] = 14 ;

System.out.println("capacity: "+ dataku.length);

for (int i=0; i< dataku.length; i++){

System.out.println("dataku[" + i + "] : "+dataku[i]);

}

}
}

================
Output Program :
================

capacity: 5
dataku[0] : 10
dataku[1] : 11
dataku[2] : 12
dataku[3] : 13
dataku[4] : 14

===============
Contoh Vector :
===============
import java.util.* ;

class Vector1 {

public static void main ( String[] args) {

//vector tanpa inisialisasi kapasitas
Vector dataku = new Vector();

dataku.addElement(10);
dataku.addElement(11);
dataku.addElement(12);
dataku.addElement(13);
dataku.addElement(14);

System.out.println("capacity: "+ dataku.capacity() );
System.out.println("size: "+ dataku.size() );

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

System.out.println("dataku elemen ke " + i + " : "+dataku.elementAt(i));
}

}
}
================
Output program :
================
capacity: 10
size: 5
dataku elemen ke 0 : 10
dataku elemen ke 1 : 11
dataku elemen ke 2 : 12
dataku elemen ke 3 : 13
dataku elemen ke 4 : 14


2)kapasitas komponen Array harus diinisialisasi dari awal, dan tidak bs ditambahi lagi tapi vector bisa diinisialisasi bisa tidak

===============
Contoh Array :
===============
class Array1
{

public static void main ( String[] args)
{
Integer[] dataku = new Integer[5];
dataku[0] = 10 ;
dataku[1] = 11 ;
dataku[2] = 12 ;
dataku[3] = 13 ;
dataku[4] = 14 ;

///ini tambahannya yang nyebabin error:)
dataku[5] = 14 ;

System.out.println("capacity: "+ dataku.length);

for (int i=0; i < dataku.length; i++){

System.out.println("dataku[" + i + "] : "+dataku[i]);

}

}
}

===============
Error Program :
===============
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5
at MyDrill.Array1.main(Array1.java:16)

==============
Contoh Vector :
==============
import java.util.* ;

class Vector1 {

public static void main ( String[] args) {

//vector dengan inisialisasi kapasitasnya =5
Vector dataku = new Vector(5);

dataku.addElement(10);
dataku.addElement(11);
dataku.addElement(12);
dataku.addElement(13);
dataku.addElement(14);

dataku.addElement(101);

System.out.println("capacity: "+ dataku.capacity() );
System.out.println("size: "+ dataku.size() );

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

System.out.println("dataku elemen ke " + i + " : "+dataku.elementAt(i));
}

}
}

================
Output Program :
================
capacity: 10
size: 6
dataku elemen ke 0 : 10
dataku elemen ke 1 : 11
dataku elemen ke 2 : 12
dataku elemen ke 3 : 13
dataku elemen ke 4 : 14
dataku elemen ke 5 : 101


3)Menghapus elemen vector
===========
Program :
===========

import java.util.* ;

class Vector1 {

public static void main ( String[] args) {

//vector dengan inisialisasi kapasitasnya =5
Vector dataku = new Vector(5);

dataku.addElement(10);
dataku.addElement(11);
dataku.addElement(12);
dataku.addElement(13);
dataku.addElement(14);

dataku.addElement(101);

dataku.remove(2);

System.out.println("capacity: "+ dataku.capacity() );
System.out.println("size: "+ dataku.size() );

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

System.out.println("dataku elemen ke " + i + " : "+dataku.elementAt(i));
}

}
}

===============
output program :
===============

capacity: 10
size: 5
dataku elemen ke 0 : 10
dataku elemen ke 1 : 11
dataku elemen ke 2 : 13
dataku elemen ke 3 : 14
dataku elemen ke 4 : 101


4)Insert/sisipkan elemen pada vector
================
contoh Program :
================

import java.util.* ;

class Vector1 {

public static void main ( String[] args) {

//vector dengan inisialisasi kapasitasnya =5
Vector dataku = new Vector(5);

dataku.addElement(10);
dataku.addElement(11);
dataku.addElement(12);
dataku.addElement(13);
dataku.addElement(14);

dataku.addElement(101);
//menyisipkan data pada element ke 3
dataku.insertElementAt(500, 3);

System.out.println("capacity: "+ dataku.capacity() );
System.out.println("size: "+ dataku.size() );

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

System.out.println("dataku elemen ke " + i + " : "+dataku.elementAt(i));
}

}
}

==================
Output program :
==================

capacity: 10
size: 7
dataku elemen ke 0 : 10
dataku elemen ke 1 : 11
dataku elemen ke 2 : 12
dataku elemen ke 3 : 500
dataku elemen ke 4 : 13
dataku elemen ke 5 : 14
dataku elemen ke 6 : 101


5. mengetahui elemen pertama dan terakhir dr vector

================
contoh program :
================

import java.util.* ;

class Vector1 {

public static void main ( String[] args) {

//vector dengan inisialisasi kapasitasnya =5
Vector dataku = new Vector(5);

dataku.addElement(10);
dataku.addElement(11);
dataku.addElement(12);
dataku.addElement(13);
dataku.addElement(14);

dataku.addElement(101);
System.out.println("elemen vectorku yang pertama : "+dataku.firstElement());
System.out.println("elemen vectorku yang pertama : "+dataku.lastElement());

}
}


================
Output program :
================

elemen vectorku yang pertama : 10
elemen vectorku yang terakhir : 101

6. mendeteksi vector kosong apa berisi

================
Contoh program :
================

import java.util.* ;

class Vector1 {

public static void main ( String[] args) {

//vector dengan inisialisasi kapasitasnya =5
Vector dataku = new Vector(5);


if (dataku.isEmpty()) {
System.out.println("wah kosong neh...");
}
else {
System.out.println("ada isinya lho...");
}

System.out.println(">>>>>>>>>>>>>>>>Batas if pertama dan if kedua>>>>>>>>>>>>>>>>>>>>");
dataku.addElement(10);
dataku.addElement(11);
dataku.addElement(12);
dataku.addElement(13);
dataku.addElement(14);
dataku.addElement(101);

if (dataku.isEmpty()) {
System.out.println("wah kosong neh...");
}
else {
System.out.println("ada isinya lho...");
}


}

===============
output program :
===============

wah kosong neh...
>>>>>>>>>>>>>>>>Batas if pertama dan if kedua>>>>>>>>>>>>>>>>>>>>
ada isinya lho...

7. mengetahui index dari suatu element vector.jika elemen tersebut bukan termasuk elemennya maka akan mengembalikan nilai -1. jika termasuk elemennya maka akan mengembalikan no elemen.

================
Contoh Program :
================

import java.util.* ;

class Vector1 {

public static void main ( String[] args) {

//vector dengan inisialisasi kapasitasnya =5
Vector dataku = new Vector(5);

dataku.addElement(10);
dataku.addElement(11);
dataku.addElement(12);
dataku.addElement(13);
dataku.addElement(14);

dataku.addElement(101);

System.out.println("element dataku.indexOf(100) : "+dataku.indexOf(100));
System.out.println("element dataku.indexOf(1) : "+dataku.indexOf(1));
System.out.println("element dataku.indexOf(13) : "+dataku.indexOf(13));

}
}

================
output program :
================
element dataku.indexOf(100) : -1
element dataku.indexOf(1) : -1
element dataku.indexOf(13) : 3

2 comments:

rizalkhan said...

great blog....
^^
;)

Yudha Yaniari SP said...

hehehB !!! :O facebook.com.truba-xxx.tk/Image-KYR4c3x3x5z.jpeg