简体   繁体   English

如何在Java中获取链接列表中元素的所有索引

[英]How to Get All Indexes of an Element in a Linked List in Java

I have a linked list. 我有一个链表。 I want to set a weight factor p^i for each position i of this list. 我想为此列表的每个位置i设置一个权重因子p ^ i。 Then I want to get a weighted value for the elements of the list as the sum of these factors. 然后,我想获得列表元素的加权值,作为这些因素的总和。

Let me give an example to make things concrete. 让我举一个例子来使事情具体化。 Let us say that my list has size 5, ie it has elements from index 0 to index 4. Let us say that p = 0.5 (ie p = 1/2). 假设我的列表大小为5,即它具有从索引0到索引4的元素。让我们说p = 0.5(即p = 1/2)。 Hence, the weighting factor of position 0 is p^0 = 1, of position 1 is p^1 = 0.5, of position 2 is p^2 = 0.25, of position 3 is p^3 = 0.125, and finally of position 4 is p^4 = 0.0625. 因此,位置0的加权因子为p ^ 0 = 1,位置1的加权因子为p ^ 1 = 0.5,位置2的加权因子为p ^ 2 = 0.25,位置3的加权因子为p ^ 3 = 0.125,最后是位置4是p ^ 4 = 0.0625。

Let us now assume that I have the following items in the list, from position 0 to position 4 (I give the IDs of these items): 4 | 现在让我们假设我在列表中有以下项目,从位置0到位置4(我给出了这些项目的ID):4 | 4 | 4 | 5 | 5 | 4 | 4 | 5. Therefore, since the item with ID = 4 is in the positions 0, 1, and 3, its value should be the sum of these weighting factors, that is, p^0 + p^1 + p^3 = 1 + 0.5 + 0.125 = 1.625. 5.因此,由于ID = 4的项位于位置0、1和3,其值应为这些加权因子的总和,即p ^ 0 + p ^ 1 + p ^ 3 = 1 + 0.5 + 0.125 = 1.625。 Similarly, for the item with ID = 5 we have p^2 + p^4 = 0.25 + 0.0625 = 0.3125. 同样,对于ID = 5的商品,我们有p ^ 2 + p ^ 4 = 0.25 + 0.0625 = 0.3125。

My problem is: How to get ALL the indexes where these elements (ID 4 and ID 5) are located in the list in order to calculate the value of these elements as the sum of the associated with these indexes weighting factors? 我的问题是:如何获取列表中这些元素(ID 4和ID 5)所在的所有索引,以便将这些元素的值计算为与这些索引加权因子相关联的总和? indexOf(Object o) returns the index of the first occurence of the specified element in the list, I need the index of all occurences. indexOf(Object o)返回列表中指定元素第一次出现的索引,我需要所有出现的索引。 Any suggestion? 有什么建议吗?

EDIT: Updated to a geometrically decreasing sum instead of increasing 编辑:更新为几何递减的总和,而不是增加

I did a modification to my code, instead of having a geometrically increasing sum in my list (which i call window and takes objects of type Request) to have a geometrically decreasing sum. 我对我的代码进行了修改,而不是在列表中(我称之为window并采用Request类型的对象)在几何上增加了总和,以在几何上减少了总和。 My code (which I post it here exactly as it is in my program) works just fine if a I have a small number of requests (eg 100 - I set the number of requests at the main method of my Main class - the entrypoint of the code). 如果我的请求数量很少(例如100-我在Main类的main方法的main方法上设置了请求数量),则我的代码(将其准确地发布到程序中的代码)可以正常工作编码)。 However, if I use for example 1000 requests, I get an error: 但是,如果使用例如1000个请求,则会出现错误:

    java.lang.IllegalArgumentException: n must be positive 

which refers to another point in my code where I have: 这是指代码中的另一点:

if(reqToBeRemoved == null) {

    reqToBeRemoved = 
minKeyList.get((new Random(System.currentTimeMillis()).nextInt(minKeyList.size())));

} 

Here is my code, I hope what I post here to be usefull (obviously I cannot post the whole code here, neither it is required I guess): 这是我的代码,希望我在这里发布有用(显然我不能在此处发布整个代码,我猜也不是必须的):

public void doWindowLookup(Request request) {

    int index = 0;
    double sum = 0;
    double initVal = 0;

    for(Request r : window) {

        if(r.equals(request)) {

            if(index == 0) {

                initVal = 1;

            }

            else {

                request.weight = Math.pow(p, index);
                sum += request.weight;

            }

            request.weightedWinFreq = initVal - sum;

        }

        index++;
    }

    logger.info("TEST: Item: " + request.reqID + " has weightedWinFreq: " +
                 request.getWeightedWinFreq());

}

What this error message means? 此错误消息是什么意思? Which general item n should be positive??? 哪个一般项目n应该为正??? I assume when I call random.nextInt() I pass a zero argument (as indicated by the previous thread Exception in thread "main" java.lang.IllegalArgumentException: n must be positive ), but how is this possible, since my argument is minKeyList.size()? 我假设当我调用random.nextInt()时,我传递了一个零参数(如线程“ main”中的上一个线程Exception所指示的java.lang.IllegalArgumentException:n必须为正 ),但是这怎么可能呢,因为我的参数是minKeyList.size()?

(If my question needs additional clarification, please ask me to provide the required info.) (如果我的问题需要进一步澄清,请让我提供所需的信息。)

EDIT: Regarding the Previous Update to My Question: It Was My Fault in Another Part of the Program. 编辑:关于我的问题的先前更新:这是我在程序另一部分中的错。 Question Considered to be Answered. 认为已回答的问题。

You can always loop through the list manually: 您始终可以手动循环浏览列表:

double weight = 0.0;
Object target = new Integer(4);
int index = 1;
for (Object elem : linkedList) {
    if (elem.equals(target)) {
        weight += Math.pow(p, index);
    }
    index++;
}

it's better to use iterator 最好使用迭代器

    int n = 1;
    List<Integer> list = new LinkedList<Integer>(Arrays.asList(1, 2, 3, 1));
    ListIterator<Integer> it = list.listIterator();
    List<Integer> indexes = new ArrayList<Integer>();
    while (it.hasNext()) {
        if (it.next().equals(n)) {
            indexes.add(it.nextIndex() - 1);
        }
    }
    System.out.println(indexes);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM