d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Quick Java Question
Add Reply New Topic New Poll
Member
Posts: 1,307
Joined: Apr 9 2007
Gold: 1,676.45
Nov 21 2014 05:27pm
Below is what my instructions are telling me to do:

Quote
append(double[] d), append(int[] i), append(long[] lo), append(float[] f)
—These methods should create a new double array containing exactly those elements of data followed by those of the array passed as parameter.
A reference to this array should be assigned to data. If the parameter is null, then the method should do nothing (no new array created).


I'm a little confused on what I need to do, this is the code that I have created so far, any help would be great. Thanks

Code
public void append(double[] d)
{
if(data.length != 0)
{
tempData = Arrays.copyOf(data, data.length+1);
for(int i=0;i<data.length;i++)
{
tempData[i] = d[i];
}
data = tempData;
}
}


I'm just don't understand this part "containing exactly those elements of data followed by those of the array passed as parameter"
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 21 2014 05:30pm
Quote

I'm just don't understand this part "containing exactly those elements of data followed by those of the array passed as parameter"


suppose this.data contains [1, 2, 3]

and i call this.append([4, 5, 6])

this.data should now contain [1, 2, 3, 4, 5, 6]

the 1, 2, 3 is "exactly those elements of data"
4, 5, 6 is "those of the array passed as a parameter"
Member
Posts: 1,307
Joined: Apr 9 2007
Gold: 1,676.45
Nov 21 2014 05:37pm
Ok, so I'm just adding the data passed from the parameter to the length of the original array, and creating a new array from that then. Is that correct?
Member
Posts: 3,099
Joined: Nov 26 2006
Gold: 0.10
Nov 25 2014 07:54pm
Nope! What you're being asked to do is called polymorphism and java deals with it very well!

All you have to do is something like this:

Code
public static void main(String[]args)
{
int[] i = {0,1};
double[] d = {0,1};

append(d); // Will call the method that uses an array double in its parameter
append(i); // Calls the method that takes int arry
}

static void append(double[] d)
{
// do code
}
static void append(int[] i)
{
// do code
}


I'd also recommend reading up on polymorphism here: https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Nov 25 2014 08:05pm
Quote (Wolfe @ Nov 25 2014 08:54pm)
Nope! What you're being asked to do is called polymorphism and java deals with it very well!

All you have to do is something like this:

Code
public static void main(String[]args)
{
  int[] i = {0,1};
  double[] d = {0,1};
 
  append(d); // Will call the method that uses an array double in its parameter
  append(i); // Calls the method that takes int arry
}

static void append(double[] d)
{
  // do code
}
static void append(int[] i)
{
  // do code
}


I'd also recommend reading up on polymorphism here: https://docs.oracle.com/javase/tutorial/java/IandI/polymorphism.html


i'm sure you're trying to help, but you're way off topic. you're gonna end up confusing OP
1) that's not polymorphism.
2) his assignment has nothing to do with polymorphism
3) the point of the assignment is to append the new array to the existing one, which your code snippet has nothing to do with

This post was edited by carteblanche on Nov 25 2014 08:09pm
Member
Posts: 23,337
Joined: Nov 2 2006
Gold: 7,528.30
Nov 26 2014 06:30pm
Yeah, and it should be relatively easy from what OP has, only

int [] tempData = new int[data.length+ i.length - 1]

since it could be adding more than one element

and just off the top of my head, use two for loops to write the correct elements to the correct index? Not sure if you can do this a simpler way.

i wrote this as

data = existing array
i = input array

This post was edited by ForTheMathematics on Nov 26 2014 06:31pm
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll