-
Array
Code:
package arrayMainFrm;
public class SimpleArray
{
public static void main ( String Args[] )
{
int myArray[] = {1,6,245,72,4,6,1,1,1};
myArray = checkA(myArray[]);
for ( int i = 0;i<myArray.length;i++)
System.out.println(myArray[i]);
}
public static int checkA(int newArray[])
{
for ( int i = 0;i<newArray.length;i++)
newArray[i]=newArray[i]*2;
}
}
Red line = illegal start of expression :rolleyes:
-
You're trying to assign an integer to an array of integers.
-
ok but the blue is what I think I did not put but it still do not work :(
Code:
package arrayMainFrm;
public class SimpleArray
{
public static void main ( String Args[] )
{
int myArray[] = {1,6,245,72,4,6,1,1,1};
myArray = checkA(myArray[]);
for ( int i = 0;i<myArray.length;i++)
System.out.println(myArray[i]);
}
public static int[] checkA(int newArray[])
{
for ( int i = 0;i<newArray.length;i++)
newArray[i]=newArray[i]*2;
}
}
-
Once an array is defined like myArray, you should never use the syntax 'myArray[]'. myArray[i] makes sense, but not myArray[].
So to answer your question, you might solve all your problems by saying:
myArray = checkA(myArray);
However, even that is a little scary because myArray on the left and myArray on the right point to the exact same location in memory. It may work out just fine, but be on the cautious side when debugging your results.
cudabean
-
I do not see if you have took a look to the code but I just wanted to try to double the value of each value of the array by using a Function... Is that the good way with Java ?
-
Well nevermind. I *was* thinking that instead of:
myArray = checkA(myArray[]);
I'd create:
int[] outArray = checkA(myArray[]);
myArray = outArray;
But upon reflection I decided that that is a dumb idea.
cudabean
-
Code:
public static void main ( String Args[])
{
int myArray[] = {1,6,245,72,4,6,1,1,1};
int myArray[] = new checkA(myArray[]);
for ( int i = 0;i<myArray.length;i++)
System.out.println(myArray[i]);
}
public static int[] checkA(int newArray[])
{
for ( int i = 0;i<newArray.length;i++)
newArray[i]=newArray[i]*2;
}
Ok anyone can tell me what is not good now ?
-
Code:
int myArray[] = new checkA(myArray[]);
checkA is not a class, you can't instantiate it like it is one.
-
Do you have the solution to it ? Because I tried a lots of thing and I have all the time an error :(
-
Just remove the 'new'. Didn't this, as I had posted above, work?
myArray = checkA(myArray);
cudabean
-
Thx you all, thx Cudabean that worked but I changed something to return:
Code:
package DoubleArrPrjMain;
public class ArrayFunction {
public static void main ( String Args[] )
{
int myArray[] = {1,6,245,72,4,6,1,1,1};
myArray = checkA(myArray);
for ( int i = 0;i<myArray.length;i++)
System.out.println(myArray[i]);
}
public static int[] checkA(int newArray[])
{
for ( int i = 0;i<newArray.length;i++)
newArray[i]=newArray[i]*2;
return newArray;
}
}