java - 我无法从Java中的数组列表中删除最后一个元素

标签 java arrays

我的程序包含 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 类。类(class)列出了我们的名字和标记

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 Collection 类。

关于java - 我无法从Java中的数组列表中删除最后一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10699571/

相关文章:

java - Groovy 与 Java 语法差异

c - 如何从函数中取回指针的指针

java - 如何将嵌套数组的 json 字段转换为 Java 实体?

c++ - 创建字符串数组/vector 并找出其中所有字符串长度的有效方法

java - 启动缓慢 - Eclipse e4

Java:我该如何关闭包裹在 SLF4J 外观中的异步附加程序?

arrays - MIPS-读取整数错误

c - 哪些 C 编译器有指针减法下溢?

java - 无法通过 Java VM 选项配置 JNDI 读取和连接超时属性

java - HSQLDB HAVING 对象未找到