|
does ayone know code for the following?
1. returns the integer array it is passed with each of the elements tripled.
2. copies the integer array it is passed and returns the copy with each of the elements tripled.
3. returns void but triples all the values of the integer array it is passed.
|
|
|
public class ArrayTest
{
public static void main(String[] args)
{
int[] firstFive = { 1, 2, 3, 4, 5 };
printArray("from getTriples", getTriples(firstFive));
int[] tripled = getTripledCopy(firstFive);
printArray("from getTripledCopy", tripled);
int[] vals = { 2, 3, 4, 5 };
triple(vals);
printArray("from triple", vals);
}
private static int[] getTriples(int[] intArray)
{
for(int j = 0; j < intArray.length; j++)
intArray[j] *= 3;
return intArray;
}
private static int[] getTripledCopy(int[] in)
{
int[] out = new int[in.length];
for(int j = 0; j < out.length; j++)
out[j] = in[j] * 3;
return out;
}
private static void triple(int[] source)
{
for(int j = 0; j < source.length; j++)
source[j] *= 3;
}
private static void printArray(String id, int[] n)
{
System.out.print(id + " = [");
for(int j = 0; j < n.length; j++)
{
System.out.print(n[j]);
if(j < n.length-1)
System.out.print(", ");
}
System.out.print("]\n");
}
}
|
|
|
|
|
|
|
|
|
|