5 FUNGSI PADA ARRAY DAN CONTOH CODING PADA JAVA



5 Fungsi pada Array dan Contoh Codingnya pada Java

1. toString
java.util.Arrays.toString (int []) metode mengembalikan representasi string dari isi int array yang ditentukan. Representasi string terdiri dari daftar elemen array itu, diapit oleh kurung siku ("[]"). Elemen yang berdekatan dipisahkan oleh karakter "," (koma diikuti dengan spasi).

Contoh Codingnya pada Java
package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo1 {

  
public static void main(String[] args) {

   
// initializing int array
    int[] i1 = new int[] { 33, 12, 98 };

   
// let us print all the elements available in list
    System.out.println("The array is:");
   
for (int number : i1) {
      System.out.println(
"Number = " + number);
    }
  
    System.out.println(
"The string representation of array is:");
    System.out.println(Arrays.toString(i1));   
  }
}

Hasil Outputnya
The array is:
Number = 33
Number = 12
Number = 98
The string representation of array is:
[33, 12, 98]

2. addAll
addAll digunakan untuk menggabungkan 2 string menjadi 1.

Contoh Coding pada Java
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class Main {
  
public static void main(String args[]) {
      String a[] = {
"A", "E", "I" };
      String b[] = {
"O", "U" };
      List list =
new ArrayList(Arrays.asList(a));
      list.addAll(Arrays.asList(b));
      Object[] c = list.toArray();
      System.out.println(Arrays.toString(c));
   }
}

Hasil Outputnya
[A, E, I, O, U]

3. Pendeklarasian Array dengan double kemudian dilakukan sorting sesuai urutanya.

Contoh Coding pada Java
package com.tutorialspoint;

import java.util.Arrays;

public class ArrayDemo {

  
public static void main(String[] args) {

   
// initializing unsorted double array
    double dArr[] = {3.2, 1.2, 9.7, 6.2, 4.5};

   
// let us print all the elements available in list
    for (double number : dArr) {
      System.out.println(
"Number = " + number);
    }

   
// sorting array
    Arrays.sort(dArr);

   
// let us print all the elements available in list
    System.out.println("The sorted double array is:");
   
for (double number : dArr) {
      System.out.println(
"Number = " + number);
    }
  }
}

Hasil outputnya
Number = 3.2
Number = 1.2
Number = 9.7
Number = 6.2
Number = 4.5
The sorted double array is:
Number = 1.2
Number = 3.2
Number = 4.5
Number = 6.2
Number = 9.7

4. Mencari Nilai Maksimal dan Minimal dengan Array

Contoh Coding pada Java
import java.util.Arrays;
import java.util.Collections;

public class Main2 {
  
public static void main(String[] args) {
      Integer[] numbers = { 8, 2, 7, 1, 4, 9, 5};
     
int min = (int) Collections.min(Arrays.asList(numbers));
    
int max = (int) Collections.max(Arrays.asList(numbers));
      System.out.println(
"Min number: " + min);
      System.out.println(
"Max number: " + max);
   }
}

Hasil outputnya
Min number: 1
Max number: 9

5. pengimplementasian untuk memperpanjang array setelah inisialisasi.

Contoh Coding pada Java
public class Main4 {
  
public static void main(String[] args) {
      String[] names =
new String[] { "A", "B", "C" };
      String[] extended =
new String[5];
      extended[3] =
"D";
      extended[4] =
"E";
      System.arraycopy(names, 0, extended, 0, names.length);
     
for (String str : extended){
         System.out.println(str);
      }
   }
}

Hasil Outputnya
A
B
C
D
E

Terima kasih telah mengunjungi blog saya :)
                                                  

Komentar

  1. You should see how my acquaintance Wesley Virgin's story starts in this SHOCKING AND CONTROVERSIAL VIDEO.

    Wesley was in the military-and soon after leaving-he unveiled hidden, "mind control" secrets that the government and others used to get everything they want.

    These are the same methods tons of celebrities (especially those who "became famous out of nowhere") and elite business people used to become wealthy and famous.

    You've heard that you only use 10% of your brain.

    Really, that's because most of your BRAINPOWER is UNCONSCIOUS.

    Perhaps this thought has even taken place INSIDE your own head... as it did in my good friend Wesley Virgin's head seven years ago, while riding a non-registered, garbage bucket of a car without a driver's license and $3.20 on his debit card.

    "I'm absolutely frustrated with living paycheck to paycheck! Why can't I turn myself successful?"

    You've taken part in those questions, ain't it so?

    Your own success story is going to be written. You need to start believing in YOURSELF.

    CLICK HERE TO LEARN WESLEY'S METHOD

    BalasHapus

Posting Komentar