簡體   English   中英

我無法從Java中的數組列表中刪除最后一個元素

[英]I can't delete last element from array list in Java

我的程序包含3類Student,Aplt和Aplt3類


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類。 單擊刪除按鈕時,我想刪除數組的最后一個元素,但不會發生。

這在我看來非常可疑:

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”。 ArrayList是Java集合類。

這是您用來添加學生的代碼:

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; 

只需使用相同的想法即可刪除其中一個:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM