removing members from an ArrayList

i am adding an object "pnew" to a queue (which i am implementing as an arrayList--same thing right?) after I add it i need to back up a step so I have a "remove" method which takes off the last element in "pnew".  The problem is that when I remove the member from "pnew", it also removes it from the queue.  The queue is an ArrayList of paths, pnew is an ArrayList of integers

path pnew = new path(start);
ArrayList<path> queue = new ArrayList<path>();

Who is Participating?
Mayank SConnect With a Mentor Associate Director - Product EngineeringCommented:
'queue' is an array list of path, so there is not a direct relationship between path and 'queue'.
Mayank SAssociate Director - Product EngineeringCommented:
>> which i am implementing as an arrayList--same thing right

You mean the ArrayList implements a queue? It can....

Though it looks like you have:

/* Queue --> */ path pnew = new path(start);
/* ArrayList --> */ ArrayList<path> queue = new ArrayList<path>(); // also named as 'queue' ?

>> pnew.remove();

We need to know what is the implementation of the path class and what it does.
jpking72Author Commented:

        int source = this.findNode(a);
        int sink = this.findNode(b);
        path p = new path(curr);
        for (int row = 0; row < nodes.length; row++) {
            path pnew = queue.remove(0);
            for (int col = 0; col < nodes.length;  col++) {
                if (matrixCap[row][col] != 0) {
                    if (pnew.contains(sink))
class path
public class path {
    private ArrayList<Integer> in = new ArrayList<Integer>();
    /** Creates a new instance of path */
    public path(ArrayList<Integer> i) {
        for (int j = 0; j < i.size(); j ++) {
            in.add(j, i.get(j));
    public ArrayList<Integer> add(Integer i) {
        return in;
    public ArrayList<Integer> remove() {
        if (in.size() >0){
        return in;}
        else return in;
    public boolean contains(int sink) {
        for (int i = 0; i < in.size(); i++) {
            if (in.get(i).equals(sink))
                return true;
        return false;
    public String toString() {
        StringBuilder out = new StringBuilder();
        out.append("found path: ");
        for (int i = 0; i < in.size(); i++)  {
            out.append(" ");
            return out.toString();
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

jpking72Author Commented:
i don't understand...i rewrote the code and still the same problem:
when I update current[i], queue also changes the previous entry in the ArrayList.  I want it to add another distinct array to the end of the ArrayList every time.

ArrayList<int[]> queue = new ArrayList<int[]>();
int[] current = new int[10];

for (col = 0; col<nodes.length; col++) {
current[i] = col;
jpking72Author Commented:
Mayank SAssociate Director - Product EngineeringCommented:
If path holds Integers, maybe even the array list should be of Integers

ArrayList <Integer> queue = new ArrayList <Integer> () ;

>> hello?

Be patient - I come here in my free time and our time zones could also be different. Any reason for the C  grade?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.