Sample array code

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

/**
 * Demo class for arrays
 * @author Sacha Chua ([email protected])
 * @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

One response to “Sample array code”

  1. Brigette says:

    can you site an example of program using 4 dimensional arrays

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>