Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何禁用Java列表的清除功能_Java_Arraylist - Fatal编程技术网

如何禁用Java列表的清除功能

如何禁用Java列表的清除功能,java,arraylist,Java,Arraylist,我有一个static列表属性(例如ArrayList),它在多个方法之间传递以添加/修改元素。现在已经很少有方法了,在不久的将来还会有很多方法 因此,我需要确保没有任何方法可以清除列表。到目前为止,我已经用空方法体重写了clear()方法 我的方法如下: private static List<String> singleList = new ArrayList<String>(){ @Override public void clear() {

我有一个
static
列表属性(例如
ArrayList
),它在多个方法之间传递以添加/修改元素。现在已经很少有方法了,在不久的将来还会有很多方法

因此,我需要确保没有任何方法可以清除列表。到目前为止,我已经用空方法体重写了
clear()
方法

我的方法如下:

private static List<String> singleList = new ArrayList<String>(){
    @Override
    public void clear() {
        //here I disabled clear() with empty method 
    };
};

public static void main(String[] args) throws IOException {
    addNumber(singleList);
    addDigit(singleList);
    addSign(singleList);
    System.out.println(singleList);
}


private static void addNumber(List<String> singleList) {
    singleList.add("1");
    singleList.add("2");
}

private static void addDigit(List<String> singleList) {
    singleList.add("A");
    singleList.add("B");
    singleList.clear();// Suppose, this line wrote accidentally, and I need to prevent it.
}

private static void addSign(List<String> singleList) {
    singleList.add("+");
    singleList.add("/");
}
private static List singleList=new ArrayList(){
@凌驾
公共空间清除(){
//在这里,我使用empty方法禁用了clear()
};
};
公共静态void main(字符串[]args)引发IOException{
addNumber(singleList);
addDigit(单数表);
addSign(singleList);
System.out.println(单列表);
}
私有静态void addNumber(列表singleList){
单一列表。添加(“1”);
单一列表。添加(“2”);
}
私有静态void addDigit(列表singleList){
单一列表。添加(“A”);
单一列表。添加(“B”);
singleList.clear();//假设这行是意外写入的,我需要阻止它。
}
私有静态void addSign(列表singleList){
singleList.add(“+”);
singleList.add(“/”);
}
有更好的选择吗


请建议我。

您可以创建自己的
UnClearableList
,它扩展了
ArrayList
——然后在
UnClearableList
中,只需覆盖
clear()
方法,并且不在其中执行任何操作(或者可能引发异常,具体取决于您的设计)

然后在“我想允许添加/删除但不清除我的列表”的情况下使用UnClearableList

你的做法在技术上是正确的。使用一个特殊的不可清除列表可以让您在其他类中重用它。
如果这只是一个学校练习,那么就没有必要了。

为此,可以使用不变性。 与其改变列表,不如始终返回一个不可变的列表,其中包含您想要的元素。例如,可以使用创建生成器并添加以前的列表和/或新元素

关于
clear
方法:

保证抛出异常并保持集合不变


我将添加一个
UnsupportedOperationException
,以确保调用
clear()
的代码被更正

如果代码依赖于
clear()
工作,则不应使用该集合


如果代码不需要
clear()
它就不应该调用它。

通常的方法是创建一个新类型或重载一个现有类型(正如您所做的那样)。此外,界面
列表
明确允许禁用
清除
,因为这是一个可选操作

从JavaDoc:

/**
 * Removes all of the elements from this list (optional operation).
 * The list will be empty after this call returns.
 *
 * @throws UnsupportedOperationException if the <tt>clear</tt> operation
 *         is not supported by this list
 */
void clear();
/**
*从此列表中删除所有元素(可选操作)。
*此调用返回后,列表将为空。
*
*@如果清除操作失败,将抛出UnsupportedOperationException
*此列表不支持
*/
无效清除();
与扩展
ArrayList
相比,更好的方法是使用一个阻止清除的类组合
ArrayList

基本上,创建一个实现
List
的类,并将其所有方法调用委托给另一个
List
实例,而不是
clear

final class CannotClearList<E> implements List<E> {
  private final List<E> delegate;

  CannotClearList(List<E> delegate) {
    this.delegate = delegate;
  }

  @Override public boolean add(E element) {
    return delegate.add(element);
  }

  @Override public E get(int i) {
    return delegate.get(i);
  }

  @Override public void clear() {
    // Cannot "do nothing": that violates the definition of the
    // Collection.clear() method.
    throw new UnsupportedOperationException();
  }

  // etc, for all other methods.
}

请注意,
clear()
不是清除列表的唯一方法。您还可以:

  • 反复调用
    删除
    
    
  • 获取一个
    迭代器
    列表迭代器
    并在迭代该迭代器时调用
    remove()
  • 使用空集合(或没有公共元素的集合)作为参数调用
    retainAll(Collection)

你应该仔细考虑你是否还想防止这些情况。

使用不可变列表的方法是什么?为什么你需要禁用清晰但没有其他方法?如果你关心清晰(),你为什么不关心ReaveE(?)?@ ReVixdiHAZUM,关于<代码> SunLelist.SaltIST(0,SunLelist.siz())。“不执行任何操作”违反了“此方法返回后集合将为空”的约定。该方法应抛出一个
UnsupportedOperationException
。这意味着更改使用它的所有其他代码,在这种情况下,您只需将
clear()
替换为设置
emptyList()
@PeterLawrey如果要使用clear,那么是的。但是OP说这些方法应该添加元素,在这种情况下,这些元素应该返回相同的不可变列表,或者是另一个。到目前为止,我还没有使用不可变列表,但我猜每次发生任何修改时它都会返回新对象。在mycase中,允许添加/mod;只是为了使re没有方法可以清除列表。@rev_dihazum在这种情况下,我建议您抛出一个异常以删除
clear()
如果不应该调用,请从代码中删除。@rev_dihazum谢谢。我已经做了同样的事情,只要看看我的代码;我使用匿名类完成了。是的,刚刚编辑了我的答案,解释在外部类中执行此操作可以在其他类中重用。您的技术也正确谢谢,到目前为止,您的答案最适合我的要求,所以我接受它作为答案。对不起,我没有接受它作为答案(肯定会投赞成票),因为有很多好的概念即将到来,所以计划稍后再决定。不要介意plz@rev_dihazum没问题,我不需要代表;)谢谢你的“不介意”:)我想知道如何
singleList.subList(0,singleList.size())清除();
清除列表!请解释,为什么不调用Overrided clear()?-并尝试一下!基本上,它不调用
ArrayList
clear()
方法,而是调用
Removate(int,int)
方法。您的方法很好,但当我实现它时,我需要重写很多方法。有没有更简单的方法?当然可以。您可以重写所有方法
@Override public List<E> subList(int from, int to) {
  return new CannotClearList<>(delegate.subList(from, to));
}