|
-
Jan 4th, 2002, 04:38 PM
#1
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
-
Jan 4th, 2002, 04:48 PM
#2
Member
You're trying to assign an integer to an array of integers.
-
Jan 4th, 2002, 04:51 PM
#3
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;
}
}
-
Jan 4th, 2002, 05:06 PM
#4
Addicted Member
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
-
Jan 4th, 2002, 05:13 PM
#5
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 ?
-
Jan 4th, 2002, 05:27 PM
#6
Addicted Member
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
-
Jan 4th, 2002, 05:37 PM
#7
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 ?
-
Jan 4th, 2002, 05:40 PM
#8
Member
Code:
int myArray[] = new checkA(myArray[]);
checkA is not a class, you can't instantiate it like it is one.
-
Jan 4th, 2002, 05:42 PM
#9
Do you have the solution to it ? Because I tried a lots of thing and I have all the time an error
-
Jan 4th, 2002, 05:49 PM
#10
Addicted Member
Just remove the 'new'. Didn't this, as I had posted above, work?
myArray = checkA(myArray);
cudabean
-
Jan 4th, 2002, 05:56 PM
#11
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;
}
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|