Java不可变队列 [英] Java Immutable Queue

查看:111
本文介绍了Java不可变队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中,我有两个链接列表liperm和litemp。我想首先使用liperm的值初始化litemp,然后添加其他值。但它不起作用,因为它没有初始化它们。你能帮忙吗?

In the code below I have two linked lists liperm and litemp. I want to initialize litemp first with the values of liperm and then add other values. But it is not working as in it is not initializing them. Can you please help:

public class ExamImmutableQueueImpl<E> implements ExamImmutableQueue<E> {

   LinkedList<E> liperm = new LinkedList<E>();
   LinkedList<E> litemp = new LinkedList<E>(liperm);

   public ExamImmutableQueueImpl(LinkedList li){
       System.out.println(li.toString());
   }

   public ExamImmutableQueueImpl(){}

@Override
   public ExamImmutableQueue<E> enqueue(E e) {
       System.out.println(litemp.toString());
       litemp.add(e);

       return new ExamImmutableQueueImpl<>(litemp);
   }

   public final void setQueue(E e){
       liperm.add(e);


   }

   public void getQueue(){
       System.out.println(litemp.toString());
   }





}

主要方法是:

public static void main(String args[]){
    ExamImmutableQueueImpl<Integer> o1 = new ExamImmutableQueueImpl<Integer>();
    ExamImmutableQueue<Integer> obj;
    o1.setQueue(2);
    o1.setQueue(1);
    o1.setQueue(2);
    o1.setQueue(3);
    obj = o1.enqueue(6);

接口是:

public interface ExamImmutableQueue<E> {
public ExamImmutableQueue<E> enqueue(E e);}


推荐答案

我将从给你一个建议:把这个代码放在一边,重新开始。
在设计层面上似乎有什么问题:

I will start by giving you an advice : put this code aside and start fresh. What seems to be wrong here on a design level:


  • 你不太明白什么是不可变对象。再读一遍。不可变意味着对象状态在构造之后永远不会改变。

  • 你有几个公共方法,其中来自接口的契约只是入队。

  • 您倾向于让方法执行他们不希望做的事情。只打印的构造函数 setQueue 哪个不设置任何队列。至少要仔细选择你的名字。

  • you didn't quite understand what is a immutable object. Read again. Immutable implies the object state never change after construction.
  • You have several public methods where the contract from the interface is only "enqueue".
  • You tend to make methods do what they are not expected to do. the constructor who only prints, setQueue which doesnt set any queue. At least choose your names carefully.

路线:


  • litemp 不应该是类字段。也许不应该存在。

  • 你需要对象中的最终字段。特别是集合 liperm

  • 在构造函数中构造对象。没有做任何事情的构造函数可能没有它的位置

  • 您是否知道元素 E 是否可变或不可变?这会对您的工作产生影响。

  • 专注于实施 enqueue 。为了使事情变得更好,您还可以 Queue as interface。

  • litemp should not be a class field. Maybe should not exists.
  • you need final fields inside your object. Especially the collection liperm
  • Construct your objects in constructors. The constructor which does nothing may not have its place
  • Do you know if the element E is assumed to be mutable or immutable? This make a difference on what you can do.
  • Focus on implementing enqueue. To make things nices, you can also have Queue as interface.

注意:不可变队列似乎对我没有意义(给定队列 理论上 是)。在开始实施之前再次询问此集合的用法。

Note: An immutable queue seems to make no sense to me (given what a queue theoretically is). Ask again what is the usage of this collection before jumping in implementation.

这篇关于Java不可变队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆