java program using array

I have to write a method that will accept an array as a parameter and return the same array, but with twice as many elements in it.

I wrote the method but when calling it in the main method I have:

Arraypro stuff = new Arrayprof();

int array[] = stuff.exArray(int[] temp);

where exArray(...) is the name of the method.

The error I am getting is

.class expected and ; expected in the line

int array[] = stuff.exArray(int[] temp);

This is the method I have written just in case:

public int[] extArray(int[] temp)

{

int i;

temp= 2 * temp;

return temp;

}

I am not calling it properly .

[697 byte] By [well1a] at [2007-11-26 23:39:42]
# 1
I typed it wrong I meanpublic int[] exArray(int[] temp)int i;temp= 2 * temp; return temp; }Is my calling wrong?
well1a at 2007-7-11 15:05:42 > top of Java-index,Java Essentials,New To Java...
# 2
Could you post the entire code as you are trying to compile it? Not just a piece of it. When posting code, please use code tags: http://forum.java.sun.com/help.jspa?sec=formatting
prometheuzza at 2007-7-11 15:05:42 > top of Java-index,Java Essentials,New To Java...
# 3

public class ArrayStuff{

public static void main(String args[])

{

int array[ ] = new int[3];

for(int i = 0; i < array.length; i++)

{

System.out.println(array);

}

ArrayStuff stuff = new ArrayStuff();

int array[] = stuff.exArray(int[] temp);

//we have to enter code here to call array and return back as array.Parameter shold be array

// should print the same as before, but have an extra 3 zeroes

for(int i = 0; i < array.length; i++)

{

System.out.println(array);

}

}

public int[] exArray(int[] temp)

{

int i;

temp= 2 * temp;

return temp;

}

}

well1a at 2007-7-11 15:05:42 > top of Java-index,Java Essentials,New To Java...
# 4
When posting code, please use code tags: http://forum.java.sun.com/help.jspa?sec=formatting
prometheuzza at 2007-7-11 15:05:42 > top of Java-index,Java Essentials,New To Java...
# 5
yup sure next time i was in a hurry
well1a at 2007-7-11 15:05:42 > top of Java-index,Java Essentials,New To Java...
# 6
> yup sure next time i was in a hurryOk, then I'll (try to) answer you next time.
prometheuzza at 2007-7-11 15:05:42 > top of Java-index,Java Essentials,New To Java...
# 7
^^^^Agreed. I'm getting annoyed at how many people don't use code tags.
lethalwirea at 2007-7-11 15:05:42 > top of Java-index,Java Essentials,New To Java...
# 8

public class ArrayStuff{

public static void main(String args[])

{

int array[ ] = new int[3];

int j;

for(int i = 0; i < array.length; i++)

{

System.out.println(array[i]);

}

ArrayStuff stuff = new ArrayStuff();

int array[] = stuff.extendArray(int[] temp);

// ENTER A LINE OF CODE HERE TO CALL extendedArray

// YOU MUST SUPPLY array AS A PARAMETER AND SAVE

// WHAT IS RETURNED BACK INTO array

// should print the same as before, but have an extra three 0s

for(int i = 0; i < array.length; i++)

{

System.out.println(array[i]);

}

}

public int[] extendArray(int[] temp)

{

int i;

temp[i]= 2 * temp[i];

return temp;

}

}

well1a at 2007-7-11 15:05:42 > top of Java-index,Java Essentials,New To Java...
# 9
I did not mean to be arrogant.
well1a at 2007-7-11 15:05:42 > top of Java-index,Java Essentials,New To Java...
# 10

> I did not mean to be arrogant.

No harm done.

Ok, in your extendArray method you will need to create a new array (just as you did in the main method) but then twice as big, and return that new array.

You can get the length of the array like this:

public int[] extendArray(int[] temp)

{

int size = temp.length;

// ... your code ...

}

Good luck.

prometheuzza at 2007-7-11 15:05:42 > top of Java-index,Java Essentials,New To Java...
# 11

public int[] extendArray(int[] temp)

{

int temp[] = new int[3];

int size = temp.length;

for(int i = 0;i < size;i++)

{

temp[i]= 2 * temp[i];

}

return temp;

}

Is this correct?

well1a at 2007-7-11 15:05:42 > top of Java-index,Java Essentials,New To Java...
# 12
I am still getting an error which says:.class expected; expectedat this lineint array[] = stuff.extendArray(int[] temp);Is there an error here?
well1a at 2007-7-11 15:05:42 > top of Java-index,Java Essentials,New To Java...
# 13
I got it.
well1a at 2007-7-11 15:05:42 > top of Java-index,Java Essentials,New To Java...