English 中文(简体)
Java奇怪的参考行为?
原标题:Java strange reference behaviour?

Lets say I have the following code:

public class Collection implements CollectionInterface{

 ElementInterface[] elementArray = new ElementInterface[100];
 int amountOfElements = 0;

 public Collection()
 {

 }

 public Collection(CollectionInterface collection)
 {
  CollectionInterface tempCollection = new Collection();
  while(!collection.isEmpty())
  {
   ElementInterface element = collection.Remove().clone();
   tempCollection.Add(element.clone2());
   elementArray[amountOfElements++] = element;
  }
  collection = tempCollection;

 }

 public void Add(ElementInterface element){
     elementArray[amountOfElements++] = element;
 }

 public ElementInterface Remove(){
  ElementInterface element = elementArray[amountOfElements].clone2();
  elementArray[amountOfElements] = null;
  amountOfElements--;
  return element;
 }

 public boolean isEmpty(){
  return amountOfElements == 0;
 }

 public CollectionInterface clone()
 {
  return new Collection(this);
 }
}

不管怎样,这似乎有点奇怪,而是有的。 但是,如果我使用以下法典:

CollectionInterface collection = new Collection();
collection.Add(new Element("Foo"));
collection.Add(new Element("Bar"));
CollectionInterface collection2 = collection.clone();

第一种斜线更含有任何内容。 如何做到这一点?

最佳回答

你不能改变投入参数的提法,因为你在第二位建筑商中尝试过。

collection = tempCollection.

a) is this a syntax error, b) collection is a local variable; assigning to it will change nothing on the outside of the constructor.

问题回答

It makes perfect sense. In the constructor, which is called by clone() with the original collection as an argument, you use:

ElementInterface element = collection.Remove().clone();

因此,在你创建新收藏品时,请从原始收藏品中删除<>m>的内容。 你们不想这样做......

鉴于你看上了<条码>,只见<>条码><>>>, 仅限<>>,<条码>和<条码>,>Remove <条/条码>方法(应为<条码>附加/条码>和<条码>> 移除,以便处理各种要素——不得以非破坏性方式获取收集。 收集类型见very>/em> odd。 难道你首先要这样做,而不是使用内在收集?

EDIT:Ah——我刚才有想法。 Within, 您可以查阅你重建的藏书的内部......这样,你就可以以破坏性的方式复制<> from。 请通过打电话<代码>删除(现为您)重新收集资料,但当你建立你的阵列时,你可以使用:

for (int i = 0; i < amountOfElements; i++)
{
    collection.Add(elementArray[i].clone2());
}

......这将使精英们再次回头。 页: 1 ......

你们只能实施以下克隆方法:

public Object Clone() {
    Collection rv = new Collection();

    for (ElementInterface element : elementArray) {
        rv.Add(element.clone());
    }

    return rv;
}

如果有必要,你可以很容易地在建筑商中执行。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签