0

私のプログラムには、Student、Aplt、Aplt3の3つのクラスが含まれています


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

public class Student {

    String name;
    int mark;

    Student(String name, int mark) {
        this.name = name;
        this.mark = mark;
    }
} 

コンストラクター付きの学生クラス

import java.applet.Applet;
import java.awt.Graphics;
import java.awt.TextFiEld;
import java.awt.event.ActioNevent;
import java.awt.event.ActionListener;

public class Aplt extends Applet {

    TextField tf[] = new TextField[2];
    Student prs[] = new Student[0];
    ActionListener ins;

    public void init() {
        tf[0] = new TextField("name?", 10);
        tf[1] = new TextField("mark", 5);
        add(tf[0]);
        add(tf[1]);
        tf[1].addActionListener(ins = new Ins());
    }

    class Ins implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            String n = tf[0].getText();
            int nt = Integer.parseInt(tf[1].getText());
            Student help[] = new Student[prs.length + 1];
            System.arraycopy(prs, 0, help, 0, prs.length);
            help[help.length - 1] = new Student(n, nt);
            prs = help;
            tf[0].setText("next name");
            tf[1].setText("next mark");
            repaint();
        }
    }

    public void paint(Graphics g) {
        for (int i = 0; i < prs.length; i++) {
            g.drawString(prs.name, 10, 50 + 12 * i);
            g.drawString(prs.mark + "", 130, 50 + 12 * i);
        }
    }
}

これがapltクラスです。クラスは私たちに与えられた名前とマークをリストします

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

public class Aplt3 extends Aplt {

    Button b1, b2;

    public void init() {
        super.init();
        b1 = new Button("remove");
        b1.addActionListener(new B1());
        add(b1);
    }

    class B1 implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            int i;
            for (i = 0; i < prs.length; i++) {
                Student help[] = new Student[prs.length - 1];
                System.arraycopy(help, 0, help, 0, help.length);
            }
            repaint();
        }
    }
}

aplt3クラス。ここでは、クリックして削除ボタンをクリックしたときに配列の最後の要素を削除したいのですが、実行されません。

4

2 に答える 2

2

これは私には非常に疑わしいようです:

for ( i=0 ; i<prs.length; i++) { 
Student help[] = new Student[prs.length-1]; 


System.arraycopy(help, 0, help, 0, help.length); 

} 

配列ヘルプをそれ自体に、配列の完全なサイズでコピーします。さらに、見栄えの悪いループでこれを行います。私はあなたが探しているのはこのようなものだと思います:

Student help[] = new Student[prs.length-1]; 
System.arraycopy(prs, 0, help, 0, help.length); 

ループはまったくありません。prsが空でないことを確認してください。空でない場合、問題が発生します。

ちなみに、括弧は「ArrayList」ではなく「array」を示します。AnArrayListはJavaコレクションクラスです。

于 2012-05-22T10:01:25.880 に答える
1

生徒を追加するために使用するコードは次のとおりです。

Student help[] = new Student[prs.length+1]; 
System.arraycopy(prs, 0, help, 0, prs.length); 
help[help.length-1]= new Student (n,nt); 
prs=help; 

同じアイデアを使用して1つを削除します。

Student help[] = new Student[prs.length-1]; 
System.arraycopy(prs, 0, help, 0, help.length); 
prs=help; 
于 2012-05-22T10:06:34.940 に答える