简体   繁体   中英

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.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:

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

It would be nice if you clarified you question better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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