Jump to content
Sign in to follow this  
Firestarter

Using Array

Recommended Posts

Salam semua,

Aku buat array tapi aku tak tau camner nak letak nilai untuk setiap variable.

 String[] list = { "Choose Value", "RM10", "RM30", "RM50","RM100"};

Cam RM10 tu aku nak letak double 10.0

Camner erkk?

Share this post


Link to post
Share on other sites

list takleh buat semua tu

kalau nak, kena buat guna HashMap()

and tak mungkin kita dapat assign any value kepada object String[1], melainkan kita terpaksa replace value String "RM10" tu.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication4;

import java.util.*;

/**
 *
 * @author user_sharil
 */
public class NewClass {

public static void main(String []args){
    Map map = new HashMap();
    
    map.put("RM10", 10.00);
    map.put("RM20", 20);
    map.put("RM30", "tiga puluh");
    
    System.out.println(map.get("RM10"));
    System.out.println(map.get("RM20"));
    System.out.println(map.get("RM30"));
    
    }
}

Share this post


Link to post
Share on other sites

list takleh buat semua tu

kalau nak, kena buat guna HashMap()

and tak mungkin kita dapat assign any value kepada object String[1], melainkan kita terpaksa replace value String "RM10" tu.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication4;

import java.util.*;

/**
 *
 * @author user_sharil
 */
public class NewClass {

public static void main(String []args){
    Map map = new HashMap();
    
    map.put("RM10", 10.00);
    map.put("RM20", 20);
    map.put("RM30", "tiga puluh");
    
    System.out.println(map.get("RM10"));
    System.out.println(map.get("RM20"));
    System.out.println(map.get("RM30"));
    
    }
}
Best juga cara tu. Aku juga cadangkan untuk guna Map. Ini version Java 5:
Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
map.put("RM10", new BigDecimal(10.00));

Dan untuk nilai wang lebih sesuai guna BigDecimal.

Share this post


Link to post
Share on other sites

Aku dah dpt cara dia..Ni contohnya:

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;

public class MapCombo {
    private JPanel getContent() {
        final Map<String, Customer> map = getMap();
        JComboBox combo = new JComboBox();
        Set<String> keys = map.keySet();
        Iterator<String> it = keys.iterator();
        while(it.hasNext()) {
            String key = it.next();
            combo.addItem(key);
        }
        combo.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                JComboBox combo = (JComboBox)e.getSource();
                String key = (String)combo.getSelectedItem();
                Customer customer = map.get(key);
                System.out.println(customer);
            }
        });
        JPanel panel = new JPanel();
        panel.add(combo);
        return panel;
    }

    private HashMap<String, Customer> getMap() {
        Customer[] customers = new Customer[4];
        customers[0] = new Customer("Helen", "Oregon", 12);
        customers[1] = new Customer("Paul",  "Idaho",  99);
        customers[2] = new Customer("Sue",   "Texas",  22);
        customers[3] = new Customer("Oscar", "Utah",   42);
        HashMap<String, Customer> map = new HashMap<String, Customer>();
        for(int i = 0; i < customers.length; i++) {
            String key = customers[i].name;
            Customer value = customers[i];
            map.put(key, value);
        }
        return map;
    }

    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new MapCombo().getContent());
        f.setSize(200,100);
        f.setVisible(true);
    }
}

class Customer {
    String name;
    String location;
    int idNumber;

    Customer(String name, String loc, int id) {
        this.name = name;
        location = loc;
        idNumber = id;
    }

    public String toString() {
        return "Customer[name:" + name +
                       " location:" + location +
                       " idNumber:" + idNumber + "]";
    }
}

Share this post


Link to post
Share on other sites

Best juga cara tu. Aku juga cadangkan untuk guna Map. Ini version Java 5:

Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
map.put("RM10", new BigDecimal(10.00));

Dan untuk nilai wang lebih sesuai guna BigDecimal.

nice generic type. ehe aku jarang guna cara ni.

tapi bagus untuk data type nyer validation.

Share this post


Link to post
Share on other sites

sama sama :D

ala byk lagi orang yang pandai sini. mungkin tak aktif je.

nak kata pandai tu tak la sangat, tapi tahu la EJB tu

even aku keje pun guna EJB, yang frameworknya hampir hampir struts.

dan sebenarnya aku keje pakai eclipse (team aku nyer policy) . baru 2 - 3 bulan ni aku start berjinak ngan netbeans (saja saja try).

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...