Raised This Month: $44 Target: $400
 11% 

How to Safely Swap All Data in 2 ArrayLists


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
sorallll
Senior Member
Join Date: Oct 2018
Old 08-31-2022 , 23:18   How to Safely Swap All Data in 2 ArrayLists
Reply With Quote #1

PHP Code:
ArrayList a = new ArrayList();
ArrayList b = new ArrayList();
ArrayList temp a;
b;
temp

Last edited by sorallll; 08-31-2022 at 23:21.
sorallll is offline
Marttt
Veteran Member
Join Date: Jan 2019
Location: Brazil
Old 08-31-2022 , 23:20   Re: How to Safely Swap All Data in 2 ArrayLists
Reply With Quote #2

Clone ?
__________________
Marttt is offline
Earendil
Senior Member
Join Date: Jan 2020
Location: Spain
Old 09-20-2022 , 21:50   Re: How to Safely Swap All Data in 2 ArrayLists
Reply With Quote #3

Quote:
Originally Posted by sorallll View Post
PHP Code:
ArrayList a = new ArrayList();
ArrayList b = new ArrayList();
ArrayList temp a;
b;
temp
You can't swap objects like you would be swapping primitives, because a = b doesn't copy the ArrayList, only copies the reference. Doing that would mess it up, the best thing to do is to modify the ArrayLists with the clone method:
PHP Code:
stock void SwapArrayLists(ArrayList a1ArrayList a2)
{
    
ArrayList buffer a1.Clone();
    
a1 a2.Clone();
    
a2 buffer.Clone();
    
delete buffer;

__________________
>>My plugins<<
>>GitHub<<

Last edited by Earendil; 09-20-2022 at 21:50.
Earendil is offline
Dragokas
Veteran Member
Join Date: Nov 2017
Location: Colony of USA
Old 09-21-2022 , 15:31   Re: How to Safely Swap All Data in 2 ArrayLists
Reply With Quote #4

The question is not clear.
if "a" and "b" are global variables, used everywhere in code without caching them (like in some statics), you don't need to swap Data, the method in sorallll post is completely safe to exchange the links only.
If links are additionally pre-cached somewhere, the Clone method is your case.
__________________
Expert of CMD/VBS/VB6. Malware analyst. L4D fun (Bloody Witch)
[My plugins] [My tools] [GitHub] [Articles] [Donate]
Dragokas is offline
sorallll
Senior Member
Join Date: Oct 2018
Old 09-21-2022 , 15:34   Re: How to Safely Swap All Data in 2 ArrayLists
Reply With Quote #5

Quote:
Originally Posted by Dragokas View Post
The question is not clear.
if "a" and "b" are global variables, used everywhere in code without caching them (like in some statics), you don't need to swap Data, the method in sorallll post is completely safe to exchange the links only.
If links are additionally pre-cached somewhere, the Clone method is your case.
global variables
sorallll is offline
Forgetest
Member
Join Date: Aug 2020
Old 09-26-2022 , 08:45   Re: How to Safely Swap All Data in 2 ArrayLists
Reply With Quote #6

Treating handles as C pointers may help with your understanding.
In C given 2 pointers, swapping is simply changing what they points to.

But if you indeed want the contents of the two handles to be swapped:
  1. Clone() doesn't help because you will eventually changing the pointer again
  2. Instead you should fill both ArrayList with contents of the other cell by cell

Bet there's better algorithm but this should go as straight as it shows.
PHP Code:
void SwapADTArray(ArrayList a1ArrayList a2)
{
    
int blocksize a1.BlockSize;
    if (
blocksize != a2.BlockSize)
    {
        
ThrowError("");
    }
    
    
int len1len2;
    
len1 a1.Length;
    
len2 a2.Length;
    
    
ArrayList temp a2.Clone(); // save a2 first
    
    
a2.Resize(len1);
    
    for (
int i 0len1; ++i// copy a1 contents to a2
    
{
        for (
int j 0blocksize; ++j)
        {
            
a2.Set(ia1.Get(ij), j);
        }
    }
    
    
a1.Resize(len2);
    
    for (
int i 0len2; ++i// copy a2 contents to a1
    
{
        for (
int j 0blocksize; ++j)
        {
            
a1.Set(itemp.Get(ij), j);
        }
    }
    
    
delete temp;

Forgetest is offline
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -4. The time now is 01:37.


Powered by vBulletin®
Copyright ©2000 - 2022, vBulletin Solutions, Inc.
Theme made by Freecode