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: -
public void expand(int a[])
-
{
-
-
}
-
which approach would be best to use in this situation?
1 3926
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: -
public void expand(int a[])
-
{
-
-
}
-
which approach would be best to use in this situation?
1)You need a second parameter for expand and a return type - 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) - int[] b = new int[(a.length + 5)];
3)To set the first 5 entries you use a loop - for(int i = 0 ;i < 5;i++) {
-
b[i] = v;
-
}
4) To copy the rest of the array, you start at position 5 in b and at position 5-5 in a. -
for(int i = 5 ;i < a.length;i++) {
-
b[i] = a[(i - 5)];
-
}
-
return b;
-
Sign in to post your reply or Sign up for a free account.
Similar topics |
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 in an array, however I don't know how many
there will be. All the example I have found for arrays only show how to
predefine the size of the array, however I would like to expand it by one
each time I need to. Can anyone let me know how to do...
|
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 a 4-byte integer followed by a stream of variable
|
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 accomplish.
// - - - - - - - - begin code - - - - - - -
typedef int sm_t;
typedef int bg_t;
sm_t sm;
bg_t bg;
|
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 accepts a void* key argument, and uses the
functions above to store this argument. It returns something (linked to
the key) that the caller can store a value in. The actual key argument
is always of the same pointer type (as seen in the caller,...
|
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
for( k = 0; k < 5; k++
a = k
| |
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?
Regards
|
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 AblistWithOccurances ()
{
super(); //Copy Constructor
}
public int expandedList(StringlistItems,int maxItems, int values)
|
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 list.
That part makes a reasonable amount of sense, as you can't actually take
items away from an Array. However, there doesn't seem to be a way to
perform the same operation in one fell swoop on a List<>.
For example:
|
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 elements which i want to keep and copy this array to the original one as follows
This function to create two arrays it depends on the parameter x.
void Econ::CreateFirms(int array,int x)
{
if(x==1)
{
FirmArray= new Firm*;
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |