Solved

removing members from an ArrayList

Posted on 2007-04-05
6
208 Views
Last Modified: 2012-05-05
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>();

queue.add(pnew);
pnew.remove();
0
Comment
Question by:jpking72
  • 3
  • 3
6 Comments
 
LVL 30

Expert Comment

by:Mayank S
ID: 18860643
>> 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.
0
 

Author Comment

by:jpking72
ID: 18861378
====================================================

        int source = this.findNode(a);
        int sink = this.findNode(b);
        curr.add(source);
        path p = new path(curr);
        queue.add(p);
       
       
       
        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) {
                    pnew.add(col);
                    if (pnew.contains(sink))
                        System.out.println(queue.toString());
                    queue.add(pnew);
                    pnew.remove();
                }
        }
           
       
    }
========================================
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) {
       
        in.add(i);
        return in;
    }
   
    public ArrayList<Integer> remove() {
       
        if (in.size() >0){
        in.remove(in.size()-1);
        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(in.get(i));
            out.append(" ");
        }    
            return out.toString();
       
   
}
}
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 500 total points
ID: 18866335
'queue' is an array list of path, so there is not a direct relationship between path and 'queue'.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:jpking72
ID: 18867304
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;
queue.add(current)
}
0
 

Author Comment

by:jpking72
ID: 18868345
hello?
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 18868854
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?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now