ArrayList changes by changing its copy - java

I am writing a code which needs to copy an arraylist but i am getting wrong answer after debugging i found that changing a copy of arraylist is changing the original arraylist but i am not able to figure out why
static void iterDeep(int level,ArrayList<ArrayList<Integer>> current){
if(current.equals(dest)){
found = 1;
printPath(current);
return;
}
if(level <= 0)
return;
for(int i=0;i<3;i++){
for(int j=0;j<2;j++){
ArrayList<ArrayList<Integer>> adj = new ArrayList<ArrayList<Integer>>(current);
if(j == 0){
for(int k=0;k<3;k++){
adj.get(k).set(i , current.get( (3+k-1)%3 ).get(i));
// Here i am changing adj values but values in current are getting changed
}
if(!map.containsKey(adj)){
map.put(adj,current);
iterDeep(level - 1 , adj);
}
}else{
for(int k=0;k<3;k++){
adj.get(k).set(i , current.get( (k+1)%3 ).get(i));
}
if(!map.containsKey(adj)){
map.put(adj,current);
iterDeep(level - 1 , adj);
}
}
}
}
return;
}

This line
ArrayList<ArrayList<Integer>> adj = new ArrayList<ArrayList<Integer>>(current);
produces a shallow copy of the list, meaning that although adding/removing elements of adj are not reflected in current, changing elements themselves have an effect on ArrayList<Integer> elements of both lists.
You need to make a method that creates a deep copy of the list, i.e. copies each individual list element into elements of adj:
static ArrayList<ArrayList<Integer>> deepCopy(ArrayList<ArrayList<Integer>> orig) {
ArrayList<ArrayList<Integer>> res = new ArrayList<ArrayList<Integer>>();
for (ArrayList<Integer> inner : orig) {
res.add(new ArrayList<Integer>(inner));
}
return res;
}

Related

error: incompatible types: List<Integer> cannot be converted to ArrayList<Integer>

I have found a few questions similar to the problem I am facing, but I couldn't find solution.
Example: Incompatible types List of List and ArrayList of ArrayList, Not able to understand how to define a List of List in java
The program should return list of lists. So, I declared a list of lists and then trying to add arraylists to it.
allsubsets = new ArrayList<List<Integer>>();
But, when I am trying to access each arraylist item from the list of lists as below, I get the error: incompatible types: List<Integer> cannot be converted to ArrayList<Integer>
for(ArrayList<Integer> subset:allsubsets)
When I try to convert the line to for(List<Integer> subset:allsubsets), it throws error that add, addAll don't exist for List type, which makes sense. Please help me understand how to access elements of list of lists in this case.
public List<List<Integer>> subsets(int[] nums) {
List<Integer> arrayList = new ArrayList<Integer>();
for(int i:nums) {
arrayList.add(i);
}
return subsets(arrayList,nums.length);
}
public List<List<Integer>> subsets(List<Integer> arrayList, int index) {
List<List<Integer>> allsubsets;
if(index == -1) {
allsubsets = new ArrayList<List<Integer>>();
allsubsets.add(new ArrayList<Integer>());
}
else {
allsubsets = subsets(arrayList, index-1);
int item = arrayList.get(index);
List<List<Integer>> moresubsets = new ArrayList<List<Integer>>();
for(ArrayList<Integer> subset:allsubsets) {
//The line above throws error as I created list of lists
List<Integer> newsubset = new ArrayList<Integer>(); //create new subset
newsubset.addAll(subset); // add all old items
newsubset.add(item); // add new item
moresubsets.add(newsubset); //add subset to moresubsets
}
allsubsets.add(moresubsets); // add to actual one
}
return allsubsets;
}
Note: If I change the return type to arraylist of arraylists, it works for me. But, I want to make it work for the list of lists
Correct way to iterate your list of list should be:
for(List<Integer> subset:allsubsets) {
instead of:
for(ArrayList<Integer> subset:allsubsets) {
List<List<Integer>> allsubsets is declared as List of List, but the implementation is unknown.
Only you know the type of nested List is ArrayList, so either change foreach to use List<Integer> or manually cast your List<Integer> to ArrayList<> (this is not preferred)
One more thing:
allsubsets.add(moresubsets); // add to actual one
This try to add a List of List (List<List<Integer>>) as element which should be List<Integer> hence compile error.
Change that statement to:
allsubsets.addAll(moresubsets);
Let's try expanding that enhanced for loop into more basic components:
for(ArrayList<Integer> subset:allsubsets) {
//The line above throws error as I created list of lists
}
// this is roughly equivalent to
Iterator<List<Integer>> it = allsubsets.iterator();
while(it.hasNext()) {
ArrayList<Integer> subset = it.next(); // Error
// Since the iterator was defined as an iterator to a List<List<Integer>>,
// it.next() will return the next element in allsubsets
// which happens to be an List<Integers>.
// You can't assign a reference of a parent type to a child. However
// the opposite is perfectly fine, assigning a reference of a child type
// to a parent.
// If we change subset to be a List<Integer> i.e.
// for(List<Integer> subset : allsubsets)
// then we are assigning a reference of a List<Integer> to a List<Integer>
// so no problem.
}
I prefer to share with you the code I did for managing the same type of Object List you are trying to handle. Hope this helps.
public static void main(String[] args) {
List<List<Integer>> allsubsets = setSubsets();
List<List<Integer>> allsubsets2 = new ArrayList<>();
allsubsets2.addAll(allsubsets);
int i= 0;
for (List<Integer> test : allsubsets2) {
System.out.println(i + " Lista");
for (Integer integer : test) {
System.out.println(integer);
}
i++;
}
}
public static List<List<Integer>> setSubsets() {
List<List<Integer>> allsubsets = new ArrayList<List<Integer>>();
List<Integer> listInteger1 = new ArrayList<>();
List<Integer> listInteger2 = new ArrayList<>();
for (int i = 0; i < 100; i++) {
listInteger1.add(i);
}
for (int i = 1010; i < 1110; i++) {
listInteger2.add(i);
}
allsubsets.add(listInteger1);
allsubsets.add(listInteger2);
return allsubsets;
}

What's wrong in my BFS shortest path implementation in Java

import java.io.*;
import java.util.*;
class Graph
{
private int V; // No. of vertices
private LinkedList<Integer> adj[]; //Adjacency Lists
private LinkedList<Integer> path[];
Graph(int v)
{
V = v;
adj = new LinkedList[v];
for (int i=0; i<v; ++i)
adj[i] = new LinkedList();
path = new LinkedList[v];
for(int i=0;i<v;++i)
adj[i]=new LinkedList();
}
void addEdge(int v,int w)
{
adj[v].add(w);
}
// prints BFS traversal from a given source s
void BFS(int s,int d)
{
boolean visited[] = new boolean[V];
LinkedList<Integer> queue = new LinkedList<Integer>();
visited[s]=true;
queue.add(s);
path[s].addLast(s);
while (queue.size() != 0)
{
s = queue.poll();
//System.out.print(s+" ");
Iterator<Integer> i = adj[s].listIterator();
while (i.hasNext())
{
int n = i.next();
if (!visited[n])
{
visited[n] = true;
queue.add(n);
path[n]=path[s];
path[n].addLast(n);
}
}
}
System.out.print("Following is the path from source to destination\n");
while(path[d].size()!=0)
{
int xyz=path[d].getFirst();
path[d].poll();
System.out.print(xyz+" ");
}
}
// Driver method to
public static void main(String args[])
{
Graph g = new Graph(4);
g.addEdge(0, 1);
g.addEdge(0, 2);
g.addEdge(1, 2);
g.addEdge(2, 0);
g.addEdge(2, 3);
g.addEdge(3, 3);
System.out.println("Following is the desired path\n");
g.BFS(2,3);
}
}
I need to get the shortest path between nodes 2 and 3.
You're attempting to add an element to a linked list that is null. You need to initialize the linked list at index s before you can add to it.
path[s] = new LinkedList();
path[s].addLast(s);
Additionally, your code is failing because you don't clone the path array when setting the value of path[n]:
path[n]=path[s];
You need to change this to:
path[n]= (LinkedList) path[s].clone();
This way the list for n won't retain a reference to the list of s. Currently, the reference is retained, so that whenever you add something to the list for n, that thing will also be added to s.
As there is no information what exactly isn't behaving as expected, it is a bit difficult to say what the error is. But I see two problems with the code:
First, you don't initialize the elements of the path. Instead, in the constructor you initialize the elements of adj twice. So, you should replace one of the lines that say
for(int i=0; i<v; ++i)
adj[i] = new LinkedList();
with
for(int i=0; i<v; ++i)
path[i] = new LinkedList();
or you can just delete the two lines as you will see now:
The second problem is that you set the different elements of path as the reference to another LinkedList and edit it:
path[n] = path[s];
path[n].addLast(n);
This has as result that every element of path uses the same LinkedList with the same elements. Instead you should create a new LinkedList containing the same elements:
path[n] = new LinkedList(path[s]);
path[n].addLast(n);
EDIT: As Soggiorno said in his answer, you must at least initialize path[s] at the beginning of the BFS-method before adding an element to it:
path[s] = new LinkedList();
path[s].addLast(s);
If you do the initialization for all the elements of path this isn't needed, of course.

Problems while iterating over an ArrayList of ArrayLists

I'm trying to iterate over an ArrayList of ArrayLists - but somehow everything fails on me and I don't understand the error message.
The error is:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.util.ArrayList
I've tried using a regular for(int i; i < lists.length; i++) but get the same error. All I want to do is check if any of the ArrayLists in "lists" contains the integer "v".
public static boolean listsContains(ArrayList<ArrayList<Integer>> lists, int v) {
boolean b = false;
for (ArrayList<Integer> list : lists) {
if (list.contains(v)) {
b = true;
} else {
b = false;
}
}
return b;
}
The actual line that causes the error is the "for (ArrayList list"...
Edited: For clarity I edited in the code with more declarative generics (which works just as little as the first code I posted unfortunately).
Edit2: Ok so it's somehow not the method itself that causes the problem so upon request here's the rest of the code that populates these lists. The code is not done but I got caught with this problem while finishing it.
public static void main(String[] args) {
Graph g = DataSource.load();
ArrayList<ArrayList<Integer>> lists = new ArrayList<ArrayList<Integer>>();
for(int i = 0; i < g.numberOfVertices(); i++) {
if(!(listsContains(lists, i))) { // add list if node is unlisted (since after first iteration one entire network is found)
listsCreate(lists, i);
}
Iterator it = g.adj(i).iterator(); // create iterator for current node's edges
if (!(it.hasNext())) { // node has no edges
listsCreate(lists, i);
} else { // node has edges, iterate through them
while(it.hasNext()) {
Edge current = (Edge) it.next();
if(!(listsContains(lists, current.to))) { // unlisted node
int index = listsIndexOf(lists, current.from);
findNetwork(g, lists.get(index), current.to);
} else {
continue; // node already listed
}
}
}
}
System.out.println("Number of connected graphs: " + lists.size());
} // Main
You did not specify the inner ArrayList's components' type. And from your log I can tell that it contains Integers:
public static boolean listsContains(ArrayList<ArrayList<Integer>> lists, int v) {
for (ArrayList<Integer> list : lists) {
if (list.contains(v))
return true;
}
return false; // No inner arrayList contains 'v'
}
EDIT:
or using Java 8 :
public static boolean listsContains(ArrayList<ArrayList<Integer>> lists, int v) {
return lists.stream().anyMatch(list -> list.contains(v));
}
Because you test:
list.contains(v)
list is of type ArrayList without inside type
v is int
replace your ArrayList by ArrayList< Integer >

Sorting a integer list without a sort command

So I have this code:
public class SortedIntList extends IntList
{
private int[] newlist;
public SortedIntList(int size)
{
super(size);
newlist = new int[size];
}
public void add(int value)
{
for(int i = 0; i < list.length; i++)
{
int count = 0,
current = list[i];
if(current < value)
{
newlist[count] = current;
count++;
}
else
{
newlist[count] = value;
count++;
}
}
}
}
Yet, when I run the test, nothing prints out. I have the system.out.print in another class in the same source.
Where am I going wrong?
EDIT: Print code from comment:
public class ListTest
{
public static void main(String[] args)
{
SortedIntList myList = new SortedIntList(10);
myList.add(100);
myList.add(50);
myList.add(200);
myList.add(25);
System.out.println(myList);
}
}
EDIT2: Superclass from comment below
public class IntList
{
protected int[] list;
protected int numElements = 0;
public IntList(int size)
{
list = new int[size];
}
public void add(int value)
{
if (numElements == list.length)
System.out.println("Can't add, list is full");
else {
list[numElements] = value; numElements++;
}
}
public String toString()
{
String returnString = "";
for (int i=0; i<numElements; i++)
returnString += i + ": " + list[i] + "\n";
return returnString;
}
}
Let's walk through the logic of how you want it to work here:
first you make a new sorted list passing 10 to the constructor, which make an integer array of size 10.
now you call your add method passing 100 into it. the method sets position 0 to 100
now you add 50, the method sets 50 in position 0 and 100 in position 1
now you add 200, which gets placed at position 2
and you add 25. which gets set to position 0, and everything else gets shuffled on down
then your method will print out everything in this list.
So here are your problems:
For the first add, you compare current, which is initialized at 0, to 50. 0 will always be less than 50, so 50 never gets set into the array. This is true for all elements.
EDIT: Seeing the super class this is how you should look to fix your code:
public class SortedIntList extends IntList
{
private int[] newlist;
private int listSize;
public SortedIntList(int size)
{
super(size);
// I removed the newList bit becuase the superclass has a list we are using
listSize = 0; // this keeps track of the number of elements in the list
}
public void add(int value)
{
int placeholder;
if (listSize == 0)
{
list[0] = value; // sets first element eqal to the value
listSize++; // incriments the size, since we added a value
return; // breaks out of the method
}
for(int i = 0; i < listSize; i++)
{
if (list[i] > value) // checks if the current place is greater than value
{
placeholder = list[i]; // these three lines swap the value with the value in the array, and then sets up a comparison to continue
list[i] = value;
value = placeholder;
}
}
list[i] = value; // we are done checking the existing array, so the remaining value gets added to the end
listSize++; // we added an element so this needs to increase;
}
public String toString()
{
String returnString = "";
for (int i=0; i<listSize; i++)
returnString += i + ": " + list[i] + "\n";
return returnString;
}
}
Now that I see the superclass, the reason why it never prints anything is clear. numElements is always zero. You never increment it because you never call the superclass version of the add method.
This means that the loop in the toString method is not iterated at all, and toString always just returns empty string.
Note
This is superseded by my later answer. I have left it here, rather than deleting it, in case the information in it is useful to you.
Two problems.
(1) You define list in the superclass, and presumably that's what you print out; but you add elements to newList, which is a different field.
(2) You only add as many elements to your new list as there are in your old list. So you'll always have one element too few. In particular, when you first try to add an element, your list has zero elements both before and after the add.
You should probably have just a single list, not two of them. Also, you should break that for loop into two separate loops - one loop to add the elements before the value that you're inserting, and a second loop to add the elements after it.

why am I getting an empty list when I am trying to merge two Sorted Linkedlists?

The lists are sorted the way they are supposed to but when I try to merge the two lists together in my makeUnion it prints out the list is empty. can anyone help me and tell my why? in main when I try SortedLinkedList merge = sortedNames1.makeUnion(sortedNames2) I get "Empty list".
public class SortedLinkedList<T extends Comparable<? super T>>
extends LinkedList<T>
{
private LinkedList<T> list; //the sorted list
//the constructor
public SortedLinkedList(LinkedList<T> in)
{
if(in.isEmpty())
{
System.out.println("Empty list");
}
if(in.size() < 2)
{
return;
}
else
{
list = new LinkedList<T>();
for(int i = 1; i < in.size(); i++)
{
T temp = in.get(i);
int j = i;
while(j > 0 && in.get(j - 1).compareTo(temp) > 0)
{
in.set(j, in.get(j-1));
j--;
}
in.set(j, temp);
}
for(T elements : in)
{
list.add(elements);
}
}
}
//return the union of the sorted linked lists this and other
public SortedLinkedList<T> makeUnion( SortedLinkedList<T> other)
{
SortedLinkedList<T> first = new SortedLinkedList<T>(other);
SortedLinkedList<T> second = new SortedLinkedList<T>(list);
SortedLinkedList<T> UnionList = null;
int i = 0;
int j = 0;
while(i<first.size() && j<second.size())
{
if(first.get(i).compareTo(second.get(j)) <= 0)
{
UnionList.add(first.get(i));
i++;
}
else
{
UnionList.add(second.get(j));
j++;
}
}
if(i == first.size())
{
for(int k = j; k<second.size(); k++)
{
UnionList.add(second.get(k));
}
}
else if(j == second.size())
{
for(int x = i; x<first.size(); x++)
{
UnionList.add(first.get(x));
}
}
return UnionList;
}
//print the items int list
public void print()
{
ListIterator itr = list.listIterator();
while(itr.hasNext())
{
System.out.println(itr.next());
}
}
}
SortedLinkedList<T> UnionList = null;
You can't call UnionList.add() if UnionList is null. You will need to allocate a new list before you can add things to it.
Actually, I think your original problem might be that SortedLinkedList both extends LinkedList and also contains an instance of a LinkedList. You should choose one or the other, but not both. Your code sometimes accesses one list, and sometimes the other, so one list appears empty because you've added items to the other list.
You don't initialize UnionList before you start using it.
SortedLinkedList<T> UnionList = null;
should read
SortedLinkedList<T> UnionList = new SortedLinkedList<T>();
As a bonus, ListIterator ought to be ListIterator<T> so that the right toString() method is used. As it is, you'll be calling Object.toString().
Because you used inheritance instead of delegation. You inherit LinkedList, and the only thing you do is define a constructor which adds the content of an unsorted list to a new one, in the appropriate order. But you don't override the size method, so this method is inherited from LinkedList, which doesn't care about your internal sorted list and thus always returns 0.
Extending a collection is, most of the time, a bad idea. In this case, it's a particularly bad idea because it's impossible to have a sorted LinkedList that respects the LinkedList API. Suppose your list contains A, B and C, and you call addFirst("Z") on it. Where will you put Z, if at the beginning, your list is not sorted anymore. If at the end, you don't respect the contract of addFirst.
Just use linked lists (instead of extending them), and sort them. You could just do :
LinkedList list = new LinkedList(someUnsortedList);
Collections.sort(list); // now the list is sorted
list.addAll(someOtherList);
Collections.sort(list); // now both lists are merged, and the resulting list is sorted.

Categories

Resources