473,320 Members | 2,104 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Array Copy And Expand

3
One more problem...

I need to write a method(Which i'm calling "expand") that takes an int array as one of its parameters and returns a new int array that is 5 elements longer. The contents of the original array need to be copied into the last elements of the new array. The first 5 elements of the new array need to be set to the value of the second parameter to expand.

So...

int[] a = new int[]{1,2,3,4,5};
int[] a2 = expand(a,-1);

should have the same result as...

int[] a2 = new int[]{-1.-1,-1,-1,-1,1,2,3,4,5};

I currently have:

Expand|Select|Wrap|Line Numbers
  1. public void expand(int a[])
  2. {
  3.  
  4. }
  5.  
which approach would be best to use in this situation?
Oct 6 '06 #1
1 3904
r035198x
13,262 8TB
One more problem...

I need to write a method(Which i'm calling "expand") that takes an int array as one of its parameters and returns a new int array that is 5 elements longer. The contents of the original array need to be copied into the last elements of the new array. The first 5 elements of the new array need to be set to the value of the second parameter to expand.

So...

int[] a = new int[]{1,2,3,4,5};
int[] a2 = expand(a,-1);

should have the same result as...

int[] a2 = new int[]{-1.-1,-1,-1,-1,1,2,3,4,5};

I currently have:

Expand|Select|Wrap|Line Numbers
  1. public void expand(int a[])
  2. {
  3.  
  4. }
  5.  
which approach would be best to use in this situation?
1)You need a second parameter for expand and a return type
Expand|Select|Wrap|Line Numbers
  1. public int[] expand(int a[], int v) {
2)You want to return an array that is larger than the one you have so you have to declare a new array.The length of this new array is easy: a.length + 5; (So we have)
Expand|Select|Wrap|Line Numbers
  1. int[] b = new int[(a.length + 5)];
3)To set the first 5 entries you use a loop
Expand|Select|Wrap|Line Numbers
  1. for(int i = 0 ;i < 5;i++) {
  2.     b[i] = v;
  3. }
4) To copy the rest of the array, you start at position 5 in b and at position 5-5 in a.
Expand|Select|Wrap|Line Numbers
  1. for(int i = 5 ;i < a.length;i++) {
  2.     b[i] = a[(i - 5)];
  3. }
  4. return b;
  5.  
Oct 6 '06 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Andrew Dixon - Depictions.net | last post by:
Hi Everyone. Bit new to Java and I have question about arrays. I have wrote some code to look for certain substrings within a larger string which work fine. I would like to store each substring...
31
by: RS | last post by:
Hi, Looking to see if the following construct is valid: typedef struct { int foo; char bar; } foobar; Basically, the idea is to have the structure above point to a message buffer that has...
11
by: truckaxle | last post by:
I am trying to pass a slice from a larger 2-dimensional array to a function that will work on a smaller region of the array space. The code below is a distillation of what I am trying to...
5
by: Stijn van Dongen | last post by:
A question about void*. I have a hash library where the hash create function accepts functions unsigned (*hash)(const void *a) int (*cmp) (const void *a, const void *b) The insert function...
1
by: moonriver | last post by:
I intend to generate a variable-length array, similar to link lists in plain C. For example, I define the following array int a Initially I assign 5 elements to the array as a = new int...
10
by: Visual Systems AB \(Martin Arvidsson\) | last post by:
Hi! Got a simple question. I am new to c# but this is not making me any sence. If i declare: string myStringArray = new string; How the heck could i fill it with more than one element? ...
1
by: bakerestates | last post by:
I am trying to copy values from an array in a parent class into an expanded array in a subclass. Here is my code so far: public class AblistWithOccurances extends Ablist public...
35
by: Lee Crabtree | last post by:
This seems inconsistent and more than a little bizarre. Array.Clear sets all elements of the array to their default values (0, null, whatever), whereas List<>.Clear removes all items from the...
3
by: aeo3 | last post by:
Hi All, Now, I am trying to build a project, I need to expand an array of pointer to classes. Moreover, this array includes some elements I want to delete them. So, I create another array, copy the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.