Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ArrayList problem 2

Status
Not open for further replies.

ajikoe

Programmer
Joined
Apr 16, 2004
Messages
71
Location
ID
Hello,

I have define two ArrayList such as this:
ArrayList intArr = new ArrayList();
ArrayList intDum = new ArrayList();

for(int i=0;i<5;i++){
intArr.Add(i*5);
}
intDum = intArr;

now if I change the value of intArr intDum also change automaticaly,it is also happened vice versa. How can I avoid this problem.

I want to create a different array with the same value.

Pujo
 
You have to do this twice. ArrayList is a reference type. this line of code "intDum = intArr;" makes the 2 arraylists point to the same memory location.

for(int i=0;i<5;i++){
intArr.Add(i*5);
}

for(int i=0;i<5;i++){
intDum.Add(i*5);
}

 
OR...

for(int i=0;i<5;i++){
intArr.Add(i*5);
intDum.Add(i*5);
}
 

Thanks,
But The intArr above only a simple example. The real one is too complex to get.
The problem is how can I transfer this two arrays.

Anyone knows about ArrayList Clone methods. How to use it?

The second problem I want to make a rectangular array in an arraylist model is that possible?

Sincerely Yours,
Pujo
 
Pujo,
What kind of data are you putting into the ArrayLists? If the data going into the ArrayLists are objects that you have created (using your own classes), then you may implement the ICloneable interface in your own classes and call the clone method for each object that you put into the arraylist. The problem with the clone method is that it creates a shallow copy of the object in question, without cloning the object that such object references (if that makes sense!). This means that if your objects have object references in them, you must implement the ICloneable interface in those classes as well.

Assuming that you have an array of the objects you will put into the array lists, and assuming that those objects implement the ICloneable interface, you could do something like this:

Code:
[COLOR=green]// yourObjArr is initalized somewhere else
// with YourClass objects[/color]
[COLOR=blue]for[/color]([COLOR=blue]int[/color] i=0; i<5; i++)
{
  intArr.Add(myObjArr[i]);
  intDum.Add([b](YourClass)[/b]myObjArr[i].[b]Clone[/b]));
}
The Clone method of the ICloneable interface returns an object so you must convert it to a YourClass type before you assign each object to the intDum array list.

Hope this helps!

JC

 
Thanks JC,

I tried to fill my ArrayList with object but it fails can you help me:
public class aku{
private int a;
public aku(int d){
a = d;
}
public int NilaiA{
get{
return a;
}
set{
a = value;
}
}
}
public class MainProj{
public static void Main(){
aku ad = new aku(5);
ArrayList intArr = new ArrayList();
intArr.Add(ad);
intArr[0].NilaiA = 4;//Fail
}
}

Sincerely Yours,
Pujo
 
Pujo,
Code:
[COLOR=blue]public class[/color] MainProj
{
  [COLOR=blue]public static void[/color] Main(){
    aku ad = [COLOR=blue]new[/color] aku(5);
    ArrayList intArr = [COLOR=blue]new[/color] ArrayList();
    intArr.Add(ad);
[COLOR=green]    // The line below fails because intArr[0]
    // returns an object. In order to use as 
    // an aku object you must first cast it 
    // into one.[/color]  
    [b]intArr[0].NilaiA = 4;[/b][COLOR=green] //Wrong code[/color]

[COLOR=green]    // The correct way is this:[/color]
    aku ad2 = (aku)intArr[0];
    ad2.NilaiA = 4;
  }
}
In the code above, the line in bold is wrong. The ArrayList class returns an object so you must convert it into an aku if you want to use it.

Now, in order for you to use the code I gave in my other post, you must implement the ICloneable interface in your aku class. Implementing this interface is very simple because it only has one method, Clone. Take a look at this code:
Code:
[COLOR=blue]public class[/color] aku [b]: ICloneable[/b]
{
  [COLOR=blue]private int[/color] a;
  [COLOR=blue]public[/color] aku(int d){
    a = d;
  }
  
  [COLOR=blue]public int[/color] NilaiA{
    [COLOR=blue]get[/color]{
      [COLOR=blue]return[/color] a;
    }
    [COLOR=blue]set[/color]{
      a = [COLOR=blue]value[/color];
    }
  }

[COLOR=green]  // Implementation of ICloneable[/color]
  [COLOR=blue]public object[/color] Clone()
  {
    [COLOR=blue]return this[/color].MemberwiseClone();
  }
}
There are two things you must keep in mind: (1)The Clone method returns an object, so before you do anything with it, you must cast it into the specific type - in this case aku; (2)The implementation of the ICloneable interface that I wrote above is the most simple one, which creates a shallow copy of the object being cloned. That means that if your object (the one you're cloning) has references to other objects, you must clone them maually in the Clone method and assign them to the object your cloning. For example, if your aku class has a property called BKU, which returns a bku object, this property will not be cloned by the Clone method above. In order to clone it properly, you must implement the ICloneable interface in the bku class and clone the BKU property (which returns a bku object) in the Clone method of the aku class. Take a look at the example below: (It is assumed that the bku class is defined somewhere else and that it implements the ICloneable interface).
Code:
[COLOR=blue]public class[/color] aku [b]: ICloneable[/b]
{
  [COLOR=blue]private int[/color] a;
  [b][COLOR=blue]private[/color] bku b;[/b]
  [COLOR=blue]public[/color] aku(int d){
    a = d;
  }
  
  [COLOR=blue]public int[/color] NilaiA{
    [COLOR=blue]get[/color]{
      [COLOR=blue]return[/color] a;
    }
    [COLOR=blue]set[/color]{
      a = [COLOR=blue]value[/color];
    }
  }
  
  [b][COLOR=blue]public[/color] bku BKU{
    [COLOR=blue]get[/color]{
      [COLOR=blue]return[/color] b;
    }
    [COLOR=blue]set[/color]{
      b = [COLOR=blue]value[/color];
    }
  }[/b]

[COLOR=green]  // Implementation of ICloneable[/color]
  [COLOR=blue]public object[/color] Clone()
  {
    aku a = [COLOR=blue]this[/color].MemberwiseClone();
[COLOR=green]    // Clone the BKU property[/color]
    [b]a.BKU = this.BKU.Clone();[/b]
[COLOR=green]    // Return the fully-cloned aku[/color]
[COLOR=blue]    return[/color] a;
  }
}

I hope all this makes sense to you!

JC

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top