Java类用于按索引存储对象

huangapple 未分类评论62阅读模式
英文:

Java class for storing objects by index

问题

抱歉,不知道如何在标题中简洁地解释。我需要一个存储<对象编号,对象>对并自动生成递增编号的Java类。如果删除了某个项目,则其编号应该重新用于下一个存储的项目。就像这样:

public class SerialStorage<T>
{
    Map<Integer, T> itemsStorage;
    Queue<Integer> deletedItemsStorage;
    int lastKnownMaxNumber;

    public SerialStorage()
    {
        itemsStorage = new HashMap<>();
        deletedItemsStorage = new ArrayDeque<>();
        lastKnownMaxNumber = 0;
    }

    /*
    存储新项目并返回其编号
     */
    public int putItem(T item)
    {
        int number;
        if (deletedItemsStorage.size() > 0)
        {
            number = deletedItemsStorage.remove();
        }
        else
        {
            number = ++lastKnownMaxNumber;
        }
        itemsStorage.put(number, item);
        return number;
    }

    /*
    根据编号获取项目
     */
    public T getItem(int number)
    {
        return itemsStorage.get(number);
    }
    
    /*
    删除项目并返回它。如果找不到此项目,则返回null
     */
    public T removeItem(int number)
    {
        T removedItem = itemsStorage.get(number);
        if (removedItem != null)
        {
            deletedItemsStorage.add(number);
        }
        return removedItem;
    }
}

Java标准库中是否有类似的内容?

英文:

Sorry, don't know how to explain it in brief in the title.
I need a java class that stores &lt;object number, object&gt; pairs generating incremental numbers automatically. If some item is deleted, the number it has shall be reused for the next stored item. Like that:

public class SerialStorage&lt;T&gt;
{
    Map&lt;Integer, T&gt; itemsStorage;
    Queue&lt;Integer&gt; deletedItemsStorage;
    int lastKnownMaxNumber;

    public SerialStorage()
    {
        itemsStorage = new HashMap&lt;&gt;();
        deletedItemsStorage = new ArrayDeque&lt;&gt;();
        lastKnownMaxNumber = 0;
    }

    /*
    Stores new item and returns it&#39;s number
     */
    public int putItem(T item)
    {
        int number;
        if (deletedItemsStorage.size() &gt; 0)
        {
            number = deletedItemsStorage.remove();
        }
        else
        {
            number = ++lastKnownMaxNumber;
        }
        itemsStorage.put(number, item);
        return (number);
    }

    /*
    Get item by it&#39;s number
     */
    public T getItem(int number)
    {
        return (itemsStorage.get(number));
    }

    /*
    Removes item and returns it. Returns null if no such item found
     */
    public T removeItem(int number)
    {
        T removedItem = itemsStorage.get(number);
        if (removedItem != null)
        {
            deletedItemsStorage.add(number);
        }
        return (removedItem);
    }
}

Is there anything like this in the java standard libraries?

答案1

得分: 0

我猜你在寻找一个ArrayList?
它们会自动编索引,当你从中间移除一个项目时,所有后续的项目将会向下滑动一个索引。

一个简单的示例:

ArrayList<String> myStrings = new ArrayList<String>();
strings.add("this is a test"); // 索引 0
strings.add("this is number two"); // 索引 1
strings.add("this is number three"); // 索引 2

System.out.println(strings.get(1));
strings.remove(1);
System.out.println(strings.get(1));

在此之后,索引 0 处将会是 "this is a test",索引 1 处将会是 "this is number three"。

英文:

I guess you´re looking for an ArrayList ?
They auto-index and they clean up so if you remove an item from the middle all the following items will slide one index down.

A brief example:

ArrayList&lt;String&gt; myStrings = new ArrayList&lt;String&gt;();
strings.add(&quot;this is a test&quot;); // index 0
strings.add(&quot;this is number two&quot;); // index 1
strings.add(&quot;this is number three&quot;); // index 2

System.out.println(strings.get(1));    
strings.remove(1);
System.out.println(strings.get(1));

After this you´ll have "this is a test" at index 0 and "this is number three" at index 1.

答案2

得分: 0

你可能需要的是一个映射(Map)。但它不会自动生成索引,这由你来完成。

Map<Integer, String> map = new HashMap<Integer, String>();
map.put(findMissingPositive(map.keySet(), map.keySet().size()), "New String");

你可以使用一个函数来获取键中缺失的最小数字。

static int findMissingPositive(int arr[], int size) 
{ 
    int i; 

    // 通过将 arr[i] 设为负数来标记 arr[i] 为已访问。注意,需要减去 1,因为索引从 0 开始,而正数从 1 开始。
    for (i = 0; i < size; i++) { 
        int x = Math.abs(arr[i]); 
        if (x - 1 < size && arr[x - 1] > 0) 
            arr[x - 1] = -arr[x - 1]; 
    } 

    // 返回第一个正数的索引值
    for (i = 0; i < size; i++) 
        if (arr[i] > 0) 
            return i + 1; // 需要加上 1,因为索引从 0 开始

    return size + 1; 
} 

https://www.geeksforgeeks.org/find-the-smallest-positive-number-missing-from-an-unsorted-array/

英文:

What you might need is a Map. But it won't generate the indexes automatically That is up to you.

Map&lt;Integer, String&gt; map = New HashMap&lt;Integer,String&gt;();
map.add(findMissingPositive(map.KeySet(), map.KeySet().size()), &quot;New String&quot;);

You might use a function that retrieves the smallest number that is missing from the keys.

static int findMissingPositive(int arr[], int size) 
    { 
        int i; 
  
        // Mark arr[i] as visited by making 
        // arr[arr[i] - 1] negative. Note that 
        // 1 is subtracted because index start 
        // from 0 and positive numbers start from 1 
        for (i = 0; i &lt; size; i++) { 
            int x = Math.abs(arr[i]); 
            if (x - 1 &lt; size &amp;&amp; arr[x - 1] &gt; 0) 
                arr[x - 1] = -arr[x - 1]; 
        } 
  
        // Return the first index value at which 
        // is positive 
        for (i = 0; i &lt; size; i++) 
            if (arr[i] &gt; 0) 
                return i + 1; // 1 is added becuase indexes 
        // start from 0 
  
        return size + 1; 
    } 

https://www.geeksforgeeks.org/find-the-smallest-positive-number-missing-from-an-unsorted-array/

huangapple
  • 本文由 发表于 2020年5月5日 03:24:56
  • 转载请务必保留本文链接:https://java.coder-hub.com/61600006.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定