0

I was asked to demonstrate a Singleton class design for my assignment. The version I submitted uses Strings and works fine, but I just can't get the reserveLane method to work properly with integers. Whenever I call the reserveLane method in the code below, it removes the element with the index of the integer passed into it instead of the element containing the value that matches the integer passed in. The program is supposed to print each message in the removeLane method once.

import java.util.*;

public class Race {
    // store one instance
    private static final Race INSTANCE = new Race(); // (this is the singleton)
    List<Integer> lanes = new ArrayList<>();
    public static Race getInstance() { // callers can get to
        return INSTANCE; // the instance
    }

    private Race() {
        lanes.add(1);
        lanes.add(2);
    }

    public void removeLane(int lane) {
        if(lanes.contains(lane)){
            lanes.remove(lane);
            System.out.println("Lane successfully reserved.");
        } else {
            System.out.println("Lane is already reserved.");
        }
    }

    public static void main(String[] args) {
        assignLane(1);
        assignLane(1);
    }

    private static void assignLane(int lane) {
        Race race = Race.getInstance();
        race.removeLane(lane);
    }
}

I'm wondering if I'm wasting my time trying to go this route or is there a way to fix it?

1
  • 1
    Try using lanes.remove(new Integer(lane)); instead Commented Dec 9, 2015 at 5:11

3 Answers 3

3
Integer integer = new Integer(lane);
lanes.remove(integer);

Your lanes is an arraylist of Integer objects, not int. Passing an int to Arraylist.remove(int index) will remove an object at that index, but if you pass an Integer object, the remove() function will delete the first occurrence of that object.

Sign up to request clarification or add additional context in comments.

1 Comment

You can see the documentation for the methods described above here
0

You are using primitive type to do your removal of element. You can convert it the Wrapper class and do it. Change the removeLane method as follows:

public void removeLane(Integer lane) {
    if(lanes.contains(lane)){
       lanes.remove(lane);
       System.out.println("Lane successfully reserved.");
}
    else{
        System.out.println("Lane is already reserved.");
    }

}

Comments

0

ArrayList Docs

E remove(int index)- Removes the element at the specified position in this list.

boolean remove(Object o) - Removes the first occurrence of the specified element from this list, if it is present.

As you have sent an int primitive type to method remove it has called remove(int index). Instead just send an Integer object and then it will call method remove(Object o) and it will work fine.

Working Code:

package stackoverflow;

import java.util.ArrayList;
import java.util.List;

public class Race {

    private static final Race INSTANCE // store one instance
            = new Race(); // (this is the singleton)
    List<Integer> lanes = new ArrayList<>();

    public static Race getInstance() { // callers can get to
        return INSTANCE; // the instance
    }

    private Race() {
        lanes.add(1);
        lanes.add(2);
    }

    public void removeLane(int lane) {
        if (lanes.contains(lane)) {
            lanes.remove((Integer) lane);
            System.out.println("Lane successfully reserved.");
        } else {
            System.out.println("Lane is already reserved.");
        }

    }

    public static void main(String[] args) {
        assignLane(1);
        assignLane(1);
    }

    private static void assignLane(int lane) {
        Race race = Race.getInstance();
        race.removeLane(lane);
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.