简体   繁体   English

在Java中添加到数组列表

[英]Adding to an array list in java

I'm trying to add items (pages that a user has viewed) to an array list, so i can then compare other pages to it. 我正在尝试将项目(用户已查看的页面)添加到数组列表中,以便随后可以将其他页面进行比较。 My theory is that i need to have a page view variable increment, but i only want to increment the variable once per picture for one session. 我的理论是我需要增加页面浏览量变量的增量,但是我只想为一个会话每张图片增加一次变量。 I don't want the case were someone can spam refresh and get more views on the picture. 我不希望有人可以刷新垃圾邮件并在图片上获得更多观看次数。

At the moment, however, a picture is added to the array list but is lost the moment you go to another picture. 然而,此刻,一幅图片已添加到阵列列表中,但是当您转到另一张图片时会丢失。 Therefore the count is always incrementing. 因此,计数始终在增加。

ArrayList<String> listOfObjects = new ArrayList<String>();
String counter = (String) session.getAttribute("counter");
listOfObjects.add(counter);
String image_id = (String) session.getAttribute("pictureName");

if (Utilities.isFirstVisit(listOfObjects, image_id) == true) {
    Utilities.IncreaseCount(out, pictureName);
    out.println("its worked");
}

Utilities method to add into array 实用程序方法添加到数组

public static boolean isFirstVisit(ArrayList<String> track, String image_id)
{
    System.out.println(track_of_first_visits);
    System.out.println(image_id);

    if (track.contains(image_id))
        return false;
    else {
        track.add(image_id);
        return true;
    }
}

I'm confused about what you're asking. 我对您的要求感到困惑。 But one thing I can tell you is that you're putting "counter" to the listOfObjects when you do: 但我可以告诉您的一件事是,您在执行以下操作时会将“计数器”添加到listOfObjects中:

listOfObjects.add(counter);

However, you're checking if the image_id is in that list later, which it won't be unless they happen to be the same number: 但是,您稍后会检查image_id是否在该列表中,除非它们恰好是相同的数字,否则它将不会存在:

if (track.contains(image_id))
    return false;

It would be nice if you clarified you question better. 如果您澄清自己的问题更好,那就太好了。

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

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