• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Swap sublist of an arraylist with the sublist of another arraylist

 
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,


I have an ArrayList of int[]. Now I want to take the first two int[] and then swap the a sublist of one int[] with another.

Can anyone please suggest how to do it?
I am more interested in the methods to be used

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm not really clear on what you're asking. Is it something like this?


Or do you want to swap elements inside the arrays?



Or something else?
 
udayshankar kintali
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
yeah something similar like

// before
list = {
[1, 2, 3],
[4, 5, 6],
[7, 8, 9]
}

// before
list = {
[1, 5,6], // swapped sublist of frist array (i.e 2,3)with sublist of second array(i.e 5,6)
[4, 2,3],
[7, 8, 9]
}

Does that make it clear?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

udayshankar kintali wrote:yeah something similar like


Does that make it clear?



Okay.

So, the first thing you have to realize is that the fact that these arrays are inside a List is totally irrelevant. Your real question is simply, "How do I swap pieces--or 'sub-arrrays'--of two arrays?"

What have you tried so far, and where are you getting stuck?

If you had


and you wanted to swap a and b, so that a held 2 and b held 1, do you know how to do that?
 
udayshankar kintali
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
view plaincopy to clipboardprint?
int a = 1;
int b = 2;

This is easy:
temp = a;
a=b;
b=temp;

Exact situation: I have an ArrayList of int[]
Now I have to swap the sublists of the first two int[]

So, I took
ArrayList<int[]> cp = new ArrayList<int[]>();

Then took two temp
ArrayList<int[]> swap1 = new ArrayList<int[]>();
ArrayList<int[]> swap2 = new ArrayList<int[]>();

After that I tried iterate over the ArrayList cp
//indexCrossoverpop = size of cp
for(int iterCross_pop=0;iterCross_pop<(indexCrossoverpop/2);iterCross_pop++){
int pos = r.nextInt(m);//getting a random number to devtermine the length of the sublist to be swapped

swap1.add(new int[m-pos]);
swap1.add(iterCross_pop, (int[])crossover_population.get(iterCross_pop));

I wanted to do the same for swap2 as well

But when I tried to iterate over the swap1 I was just getting addresses but I want to access the elements.

I hope that is clear
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

udayshankar kintali wrote:view plaincopy to clipboardprint?
int a = 1;
int b = 2;

This is easy:
temp = a;
a=b;
b=temp;



Yup. So you can apply the same technique here.

Exact situation: I have an ArrayList of int[]



Again, the fact that these arrays are inside a List is irrelevant. Just work out how to swap pieces of two arrays first, without thinking about the List.

Then took two temp
ArrayList<int[]> swap1 = new ArrayList<int[]>();
ArrayList<int[]> swap2 = new ArrayList<int[]>();



No need for any additional lists.

You have this:


and you want to swap the 2 with the 5 and the 3 with the 6. So, in other words, from index 1 through index 2, you want to swap the elements in 2 arrays.

Start with this:


Can you complete that method?

But when I tried to iterate over the swap1 I was just getting addresses but I want to access the elements.



No, you weren't getting addresses. You never have access to addresses in Java.
 
udayshankar kintali
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I was printing the Object.

// swaps element at position n in two arrays
void swap(int[] arr1, int[] arr2, int index) {
// code to swap elements at a single index goes here

for(int i = 0 ; i< arr1.lenth;i++)
{
if(i>=index)
{
temp = arr1[i];
arr1[i]=arr2[i];
arr2[i]=temp;
}



}


Isn't that right??
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

udayshankar kintali wrote:I was printing the Object.



Ah, so you were printing an array, and getting Object's toString(), which returns the object's type and hashCode. If you want to print the contents of an array, you need to iterate over it, printing each element, or call Arrays.toString() or deepToString().


// swaps element at position n in two arrays
void swap(int[] arr1, int[] arr2, int index) {
// code to swap elements at a single index goes here

for(int i = 0 ; i< arr1.lenth;i++)
{
if(i>=index)
{
temp = arr1[i];
arr1[i]=arr2[i];
arr2[i]=temp;
}



}


Isn't that right??



Close. But there's no need for a loop and no need for an if test. You're just swapping a single element.

I was trying to take it simple, one step at a time, but if you're trying to swap all the elements in some range, then yes, you'd use a loop, but you still don't need an if test.

Finally, when posting code, please UseCodeTags.(⇐click)
 
udayshankar kintali
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Without the if test how would you know from where to start the swapping. And yes, I want to swap a sublist(within a range) to be swapped.


Since, you said that logic is correct. What is the problem there


The Sysoout printed :
pos9
[I@5013582d
[I@26021b6d
[I@5013582d
[I@26021b6d

Please tell me where I am going wrong
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

udayshankar kintali wrote:Without the if test how would you know from where to start the swapping.





We don't have to use i = 0 and arr.length.

The Sysoout printed :
pos9
[I@5013582d
[I@26021b6d
[I@5013582d
[I@26021b6d



As I said, if you want to print the elements of an array, you have to iterate over the array and print them individually, or call java.util.Arrays.toString() or deepToString().
 
udayshankar kintali
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I used this to iterate over and print...but it prints the same thing. Where can I use , or call java.util.Arrays.toString() or deepToString().
 
udayshankar kintali
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Still not able to access the individual element of the array...you asked me to use toString()..it gives the output as :
[I@164f1d0d

[I@23fc4bec
How to access the array elements here?
 
udayshankar kintali
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey Jeff,

It worked.

Thanks.
 
udayshankar kintali
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff,

When we pass [][](2-D) array as a parameter to a function and in that function we make some changes, the changes will be reflected in the actual array right.
This is how I am sending a 2-D array :



So, I am actually sending the base address. So will the changes be reflected in the array itself?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

udayshankar kintali wrote:
I used this to iterate over and print...but it prints the same thing. Where can I use , or call java.util.Arrays.toString() or deepToString().



That iterates over your List. You need to iterate over the arrays.


 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

udayshankar kintali wrote:Jeff,

When we pass [][](2-D) array as a parameter to a function and in that function we make some changes, the changes will be reflected in the actual array right.



An array, whether 1D, 2D, or 255D, is an object just like any other. And just like any other object, when we pass it to a method, we're really passing a reference to it, so that both the caller and the method have references to the same object. So, just like any other object, when our method modifies the contents of the array, it is seen by the caller as well, since it is looking at the same object as our method.

So, I am actually sending the base address.



Strictly speaking, no, it's not an address. Addresses are not present in Java. It's simply a reference. Under the covers, it will most likely be implemented as an address, and you can picture it as being so, but that concept isn't strictly present in the Java language.
 
udayshankar kintali
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Jeff,

Now I have a two dimensional array which has the initial set of data and then I have an ArrayList of Integers which has some of the rows of the two dimensional arrays .And the indexes of the rows which were copied from the two dimensional array are stored in another ArrayList. The new set of ArrayList of Integers was modified and now I want to update these rows in the original 2 D array.

This is the code I used :

But I am unable to update the 2 D array with the modified rows.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

udayshankar kintali wrote: . . .

Now I have a two dimensional array . . .

No, you haven’t. There’s no such thing. You have an array of arrays.
 
udayshankar kintali
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, array of arrays. Can you suggest a solution?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic