English 中文(简体)
具有再现指数的射线仪
原标题:ArrayList with returned index on add value

我正在寻找一个类似于阿雷拉派分子的java数据结构,如果我只做一个价值论点的附加或推力,那么我就自动回到一个指数。

例如:

ArrayList<String> elements = new ArrayList<String>();

String element = "foo";
String elementTwo = "bar";

int index1 = elements.add(element); //note this does not exist, i.e. returns bool in api
int index2 = elements.add(elementTwo);

System.out.println(elements.get(index1)); //would give "foo"

我可以看到在ArrayList周围写出一个包裹的班子,它管理着每增加一次手术所加固的反响,并援引:

ArrayList.add(int index, E element)

Do you really need to write a wrapper around ArrayList for this? This seems like something simple enough to be provided out of the box somewhere?

Edit:

我需要为这一使用案例确定和独一无二的指数(钥匙)。 提出了地图,我同意。 是否有任何人了解使你自动(单独)产生价值的关键地图的执行情况? 我只是试图决定我是否需要为此执行自己的总结。

最佳回答

该要素将在清单末尾添加。 因此,您可使用<代码>elements.size()-1获取新的要素指数。

Note that this will not work reliable if multiple threads are modifying the list at the same time.

<>光线> 还注意到使用<代码>可能不是一个好的想法。 ArrayLists index as a separate ID because an elements index can change (例如,在你删除一个元素或插入一个使用add(int, Object)的新元素时)。 如果这个问题取决于你想要与指数做些什么: 如果在添加一个要素之后仅需要一段短暂的时间,而且可以确保清单在此期间不作修改,就不存在问题。 在其他情况下,即便是使用<代码>add(目标)时回归指数的方法也无助于,因为指数没有任何更新。 防止这一问题:

  • Make sure you never remove elements from the list and never add elements using add(int, Object).
  • Instead of removing elements you could also set them to null using the method set(int, null). This way no elements index will change.
  • Use some other data structure like for example a map with a custom ID like helloannalil suggests in his answer.

<>EDIT2:,我找不到适当的、随时可用的执行(但这并不意味着当然没有)。 为了提出妥善的解决办法,需要更多关于打算使用数据结构的信息,但这里有一些想法和说明:

  • If the maximum number of elements is not to large, an ArrayList could be used and the elements index represents the ID. As stated above, to remove an element it can be set to null so that no indices are changed. When inserting, positions with null values can be reused.
  • You can also use one of the two methods show in this answer: https://stackoverflow.com/a/8939049/1347968 (keywords AtomicLong or IdentityHashMap)
  • Do not depend on the "uniqueness" of Object.hashCode() or System.identityHashCode(Object) as it is not guaranteed (try it by running the example at the bottom of Suns/Oracles Bug #6321873).
问题回答

我在这种情况下所做的事情(我爱阵列主义者)就是通过询问名单的规模来达到最后指数:

String thing = "theThing";
List<String> strList = new ArrayList<String>();
strList.add(thing);
int indexOfThing = strList.size() - 1;

我说,比执行你自己的名单和公正的工作更容易。

如果你真的想要履行这一职能,你可以使用地图,但不能列入名单。

根据你的评论和经过编辑的问题,我认为你可以提出“哈希姆帕拉”这样的用途:

public class MyMap<V> extends HashMap<Integer, V> {
    private static final long serialVersionUID = 1L;
    public int add(V elem) {
        int key = System.identityHashCode(elem);
        super.put(key, elem);
        return key;
    }
}

然后在您的班子内宣布<代码>MyMap。 类似:

private MyMap<String> map = new MyMap<String>();

然后在<代码>MyMap上添加内容。 类似:

.....
.....
String element = "foo";
String elementTwo = "bar";
int index1 = map.add(element);
int index2 = map.add(elementTwo);

现在,您有<代码>index1和index2,作为您在申请一生中可使用或通过的各项指示数。 页: 1 MyMap 您所希望的指数(index1index2)将给你留下以下内容:

String elem1 = map.get(index1); // will return "foo"
String elem2 = map.get(index2); // will return "bar"
String thing = "theThing";
List<String> strList = new ArrayList<String>();
strList.add(thing);
int indexOfThing = strList.size() - 1;

如果你删除一个项目,这将不再奏效。





相关问题
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 ...

热门标签