我是靠谱客的博主 苗条苗条,最近开发中收集的这篇文章主要介绍java arraylist插入,如何在Java中添加上的ArrayList监听,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

I want to create my own implementation of ArrayList in java, that can listen when the list is changing and to do action when this happens.

From what I have read, I understand that I can't extend ArrayList and then add listener.

I want to use MyList in class as a variable with public modifier, so users can change it directly and to be done action when he changes it.

class MyList extends ArrayList.... { ... }

class UseOfMyList {

public MyList places = new MyList();

places.add("Buenos Aires");

//and to be able to do that

List cities = new ArrayList();

cities.add("Belmopan");

places = cities;

So how to create and when do add,remove or pass another list to MyList an action to be performed?

解决方案

You're not going to be able to do this by extending ArrayList, as it has no built-in notification mechanism (and, further, because it is has been declared final and thus cannot be extended). However, you can achieve your desired result by creating your own List implementation and adding your "listener" functionality vis a vis the add() and remove() methods:

class MyList{

private ArrayList list;

public MyList(){

list = new ArrayList<>();

...

}

public void add(T t){

list.add(t) {

//do other things you want to do when items are added

}

public T remove(T t){

list.remove(t);

//do other things you want to do when items are removed

最后

以上就是苗条苗条为你收集整理的java arraylist插入,如何在Java中添加上的ArrayList监听的全部内容,希望文章能够帮你解决java arraylist插入,如何在Java中添加上的ArrayList监听所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(38)

评论列表共有 0 条评论

立即
投稿
返回
顶部