1 / 39

Object Array

Object Array. Nana Ramadijanti Laboratorium Computer Vision Politeknik Elekltronika Negeri Surabaya PENS-ITS 2008. Topik. Mendeklarasikan Array Creating Array Initializing Array Array Multidimensional Array Bounds Array Resizing Copying Arrays. Mendeklarasikan Array.

cole
Download Presentation

Object Array

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object Array Nana Ramadijanti Laboratorium Computer Vision Politeknik Elekltronika Negeri Surabaya PENS-ITS 2008

  2. Topik • Mendeklarasikan Array • Creating Array • Initializing Array • Array Multidimensional • Array Bounds • Array Resizing • Copying Arrays

  3. Mendeklarasikan Array • Mengelompokkan obyek data dengan tipe yang sama • Pendeklarasian array dengan tipe primitif dan class char s[]; char[] s; Point p[]; Point[] p; • Array adalah sebuah objek • Alokasi memori dibuat dengan keyword new

  4. Array • Untuk membuat array terdapat 3 langkah • Declaration • Construction • Initialization • Deklarasi • int[] ints • double[] dubs • Dimension[] dims ; • float[][] twoDee • Pada saat pendeklarasian tidak menentukan besar array

  5. Array • Besar array ditentukan pada saat runtime, alokasi memori dilakukan dengan keyword new • int[] ints ; • ints = new int[25] ; • Pada saat array di buat, isi array diinisialisasi dengan default value.

  6. Array • Gabungan dari 3 langkah float[] diameters = {1.1f, 2.2f, 3.3f, 4.4f, 5.5f} Point[] markup = {new Point(1,5), new Point(3,3), new Point(2,3)} ; • Besar array dapat diketahui dengan nama_array.length long squares ; squares = new long[6000] ; for (int i=0 ; i<squares.length ;i++) { squares[i] = i * i ; }

  7. Contoh program

  8. Hasil running • 0 • 0 • 0 • 0 • 0 • false • false • false • false • false • false

  9. Contoh Program

  10. Hasil Program • A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

  11. Contoh Program

  12. Output • animal 0 : snake • animal 1 : kangaroo • animal 2 : wombat • animal 3 : bird

  13. Contoh Program • Lakukan modifikasi dengan melakukan sorting pada data animal

  14. Sebelum di sorting • animal 0 : snake • animal 1 : kangaroo • animal 2 : wombat • animal 3 : bird • Setelah di sorting • animal 0 : bird • animal 1 : kangaroo • animal 2 : snake • animal 3 : wombat

  15. Class Arrays • This class contains various methods for manipulating arrays (such as sorting and searching). • The methods in this class all throw a NullPointerException if the specified array reference is null.

  16. Contoh • Buat program untuk menerima masukan data nama mahasiswa yang disimpan dalam array dengan tipe String. Kemudian tampilkan data mahasiswa tersebut.

  17. Program

  18. Hasil Program

  19. Contoh Program • Lanjutkan program sebelumnya dengan menampilkan data mahasiswa dengan di sorting ascending terlebih dahulu

  20. Output Program

  21. Praktek 1 Periode pemilihan anggola legislatif 2009 diadakan pooling oleh lembaga surveyor terhadap 5 caleg dari 50 orang responden untuk mengetahui jumlah pendukung masing-masing caleg dan siapa yang mempunyai pendukung terbanyak. Buatlah Program pooling untuk menyelesaikan permasalahan tersebut. Sebagai input suara pilihan setiap responden terhadap 5 caleg (input 1/2/3/4/5). Input :(dapat menggunakan fungsi random (int)(Math.random*10%5)) Responden 1 : 1 Responden 2 : 5 … Responden 50 : 1 Output : Caleg 1 : jumlah pemilih 20 Caleg 2 : jumlah pemilih 12 Caleg 3 : jumlah pemilih 5 Caleg 4 : jumlah pemilih 3 Caleg 5 : jumlah pemilih 5 Caleg 1 paling banyak dipilih

  22. Array Multidimensional

  23. Array Multidimensional • Arrays of arrays yang bukan persegi panjang 0 1 2 3

  24. Array Multidimensional • Array dengan 4 baris dan 5 kolom 1 2 3 4 0 0 1 2 3

  25. Contoh program

  26. Hasil running • Length pada Indeks ke-0 =3 • Length pada Indeks ke-1 =5

  27. Contoh Program

  28. Hasil running • Elemen pd Dimensi ke-1 = 2 • Elemen pd Dimensi ke-2 = 3 • Elemen pd Dimensi ke-3 = 4

  29. Contoh • Terdapat sebuah array yang terdiri dari 4 baris, besar kolom tiap baris dibangkitkan secara random. Isi masing-masing dari array tersebut harus memenuhi aturan • Baris 0 : kelipatan 2 • Baris 1 : kelipatan 3 • Baris 2 : kelipatan 4 • Baris 3 : kelipatan 5 0 2 4 1 6 9 12 3 2 4 8 12 16 20 24 3 5 10 15 20 25 30 35 40

  30. KOLOM YANG DIBANGKITKAN Baris ke-0 = 1 Baris ke-1 = 8 Baris ke-2 = 7 Baris ke-3 = 4 • MENGISI MATRIK 2 3 6 9 12 15 18 21 24 4 8 12 16 20 24 28 5 10 15 20

  31. Contoh Program Output 0 1 2 3 4 5 Setelah di Array Diperbesar 0 1 2 3 4 5 6 7 8 9

  32. Copying Array

  33. Hasil Running • 1 2 3 4 5 6 • 1 2 3 4 5 6 4 3 2 1

More Related