Sample array code

| geek
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

/**
 * Demo class for arrays
 * @author Sacha Chua (sacha@free.net.ph)
 * @date 22-Feb-2004
 */

public class ReallySimpleAddressBook extends Applet implements ActionListener
{
    private Button set;
    private Button show;
    private TextField index;
    private TextField name;
    private String names[];

    public void init()
    {
        names = new String[10];
        set = new Button("Set");
        show = new Button("Show");
        set.addActionListener(this);
        show.addActionListener(this);

        index = new TextField();
        name = new TextField();
        Label label1 = new Label("Index:");
        Label label2 = new Label("Name:");

        setLayout(new GridLayout(3, 2));
        add(label1);
        add(index);
        add(label2);
        add(name);
        add(set);
        add(show);

    }

    public void actionPerformed(ActionEvent e)
    {
        if (e.getSource() == set)
        {
            int i = Integer.parseInt(index.getText());
            names[i] = name.getText();
        }
        if (e.getSource() == show)
        {
            int i = Integer.parseInt(index.getText());
            name.setText(names[i]);
        }
    }
}

../../tmp/ReallySimpleAddressBook.java

You can comment with Disqus or you can e-mail me at sacha@sachachua.com.