i have two arrays say arrOne and arrTwo. now arrOne is having 27 elements and arrTwo is vacant. On click of a button i want to copy first 10 elements of arrOne to arrTwo, then on second click, i want to add another 10 elements and the rest 7 on another click. example with code would be really helpful,,.. thnx O_o
Asked
Active
Viewed 682 times
0
-
Did you try writing something in an attempt to implement this? If so, what happens when you run it? – occulus Mar 01 '11 at 12:10
-
1Questions of the form, "Here is a specification, please give me the code" aren't very useful. "I have a problem with my code that does X" is a more answerable question. – occulus Mar 01 '11 at 12:13
-
ok.. should i use this?? [one](http://stackoverflow.com/questions/733243/how-to-copy-part-of-an-array-to-another-array-in-c) – Zaraki Mar 01 '11 at 12:17
-
Do you have c style arrays or do you have NSArray objects? – deanWombourne Mar 01 '11 at 12:20
-
2So a way for you to break this problem down is to a) work out how to hook up a button to some code you write, b) how to manipulate NSMutableArray objects (and use NSArrays) -- read the Apple API docs, c) tie it all together in some code you write, d) ask here again if you run into specific problems. – occulus Mar 01 '11 at 12:21
-
You've tagged this question with iphone and objective-c, so I took it for granted you're using objective-c for the iphone. That question you linked to is about c#. objective-c is not c#. – occulus Mar 01 '11 at 12:23
1 Answers
1
Here is how to do it:
//
// Filling first array with 20 elements
//
NSMutableArray* arrOne = [[NSMutableArray alloc] initWithObjects:nil];
NSMutableArray* arrTwo = [[NSMutableArray alloc] initWithObjects:nil];
for (int i=1; i<27; i++) {
[arrOne addObject:[NSNumber numberWithInt:i]];
}
//
// Adding 10 elements starting from initialPosition to second array
//
NSLog(@"arrOne: %@", [arrOne componentsJoinedByString:@", "]);
int initialPosition = 0; // Just change the initial, starting position to 10, 20, 27 and so on..
[arrTwo addObjectsFromArray:[arrOne objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(initialPosition, 10)]]];
NSLog(@"arrTwo: %@", [arrTwo componentsJoinedByString:@", "]);
[arrOne release];
[arrTwo release];

Andrew
- 3,874
- 5
- 39
- 67

Rafał Sroka
- 39,540
- 23
- 113
- 143