7 Replies - 240 Views - Last Post: 01 February 2013 - 12:24 PM Rate Topic: -----

#1 Tree55Topz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 06-September 12

Adding all elements of one array to another

Posted 01 February 2013 - 10:58 AM

Lets say you have array arr1 = {2,3,4,null,null,null,null} and..
arr2 = {6,7,8}
What I want to do is to add the arr2 to arr1 on top of the stack so that after the addAll method arr1 would look like..

arr1 = {2,3,4,6,7,8,null} and arr2 would be empty. I know using an arrayList would more than likely be easier but I am trying to understand the logic of using multiple for loops to iterate through both arrays. The method looks like this.

public Bag addAll(Bag b1);

The Bag is a class with a constructor:
Bag(int size){
		counter = 0;
		count_ItemsRemoved = 0;
		count_ItemsRandomlyRemoved = 0;
		collect = new Object[size];
	}

Any help would be greatly appreciated!

Is This A Good Question/Topic? 0
  • +

Replies To: Adding all elements of one array to another

#2 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10194
  • View blog
  • Posts: 37,646
  • Joined: 27-December 08

Re: Adding all elements of one array to another

Posted 01 February 2013 - 11:02 AM

If Bag has a size field, then the last element in the array is located at size-1. So place bag2.array[0] at bag1.array[bag1.size]. Then just adjust the offsets by 1.

So:
bag2.array[1] -> bag1.array[bag1.size+1]
bag2.array[2] -> bag1.array[bag1.size+2]

Do you see the pattern?
Was This Post Helpful? 1
  • +
  • -

#3 Tree55Topz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 06-September 12

Re: Adding all elements of one array to another

Posted 01 February 2013 - 11:49 AM

Yes i see the pattern, I am just confused on how to start my for loop. maybe something like..

for (int i = 0; i < bag1.size(); i++){
for (int j = bag2.size; j < bag1.size(); j++){
bag1[bag1.size()+1] = bag2[j];
}
}

guess I should try it first huh?
thank you!
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10194
  • View blog
  • Posts: 37,646
  • Joined: 27-December 08

Re: Adding all elements of one array to another

Posted 01 February 2013 - 11:51 AM

Consider a Bag with an array: {2,3,4,null,null,null,null}. Here, the size of the Bag is 3, as it has 3 elements. However, the length of the array is 7. The size is what the Bag will let the public see. A lot of dynamically sized data structures take this approach. ArrayLists double the size of the array when necessary. However, if only 10 elements have been added, the ArrayList size() is 10 while the internal array size may be 20 or 30. Does this make sense?
Was This Post Helpful? 2
  • +
  • -

#5 Tree55Topz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 06-September 12

Re: Adding all elements of one array to another

Posted 01 February 2013 - 12:11 PM

Yes I definatly understand the logic, so in addition to the for loop I should also increase the counter on the array which will change the size.
Was This Post Helpful? 0
  • +
  • -

#6 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1277
  • View blog
  • Posts: 2,846
  • Joined: 05-April 11

Re: Adding all elements of one array to another

Posted 01 February 2013 - 12:11 PM

You would only need 1 loop :)

Also remember that the first element should be copied to array[size] and not array[size+1] since arrays start at 0
Was This Post Helpful? 0
  • +
  • -

#7 Tree55Topz  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 84
  • Joined: 06-September 12

Re: Adding all elements of one array to another

Posted 01 February 2013 - 12:12 PM

Got it working. As simple as this :)
public Bag addAll(Bag b1){
		for (int i = 0; i < b1.size(); i++){
				collect[this.size()] = b1.collect[i];
				counter++;
		}return this;
	}

Was This Post Helpful? 1
  • +
  • -

#8 CasiOo  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1277
  • View blog
  • Posts: 2,846
  • Joined: 05-April 11

Re: Adding all elements of one array to another

Posted 01 February 2013 - 12:24 PM

Good job ^^

But you should properly also know how most programmers in Java does this operation
The System class offers an arraycopy method, which does exactly what you have been coding
The method is native, and I bet you it is faster than any implementation you can come up with
This is how it's defined
public static native void arraycopy(Object src,  int  srcPos,
                                        Object dest, int destPos,
                                        int length);



I think this would work
System.arraycopy(b1.collect, 0, collect, size(), b1.size());


This post has been edited by CasiOo: 01 February 2013 - 12:24 PM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1