I'm trying to swap the first element in a list with the third element in the array (not ArrayList) so that:
vector = {23, 42, 3, 42, 6};
will become:
vector = {3, 42, 23, 42, 6};
I have tried writing into my code:
int temp = vector[0];
vector[0] = vector[2];
vector[2] = temp;
This is my code in BlueJ:
public class Lab1
{
public static void main( String args[] ) {
new Lab1().run();
}
int[] vector = {23, 42, 3, 42, 6};
int temp = vector[0];
vector[0] = vector[2];
vector[2] = temp;
public void run() {
System.out.println(vector[0]);
System.out.println(vector[2]);
System.out.println(vector[vector.length-1]);
System.out.println(temp);
}
}
Can someone explain why I am getting this error coming up. I found this solution online, but it doesn't seem to work - what am I doing wrong?
EDIT:
The name of the array isn't important - I am only doing this as an exercise given to me in class.
I did not put all the vector stuff in the run() method because I am
very new to this and it did not occur to me that that was what I supposed to doI apologise for not inserting the code into the text - I am relatively new to stackoverflow and lack experience with it, so I did not think to insert the whole body of my code as text.
Regardless - I put the 3 lines of code I was trying to put before in the run() method, and it now works. Thanks for your help.
CodePudding user response:
The problem is that you are assigning a variable outside {} block, where fields initialization is expected. Try to include the swapping in your run method.
// int[] vector = {23, 42, 3, 42, 6}; either here
public static void main(String[] args) {
new Lab1().run();
}
public void run() {
int[] vector = {23, 42, 3, 42, 6}; // or here
int temp = vector[0];
vector[0] = vector[2];
vector[2] = temp;
System.out.println(vector[0]);
System.out.println(vector[2]);
System.out.println(vector[vector.length - 1]);
System.out.println(temp);
}
Please also consider renaming the variables as suggested by other members.
CodePudding user response:
When you're doing int[] vector = {...}
, you don't realize it but you are instantiating an instance member (a value that is attached to an instance of your class) rather than a local variable. The syntax is valid because in the absence of public
, protected
or private
before an instance (or static) member declaration, it uses package-private scope (all classes in the same package can see the declaration, but not any class outside it).
Then when you're trying to assign a value to vector[0]
, this is where it becomes invalid. You cannot make an assignment outside of a block, unless of course this assignment is made as part of a declaration. Similarly, you wouldn't be able to do 3 5
or System.out.println("hello")
outside of a block (note: a method counts as a block).
Below are some illustrations:
public class Test {
static {
System.out.println("Hello, I'll execute during class initialization since I'm a static block");
}
{
System.out.println("Hello, I'll execute during instantiation since I'm a non-static block");
}
System.out.println("This does not compile, cannot put anything else than a block, method, constructor or declaration at the top level of the class");
// though using package-private is generally discouraged, private should be the default, and things that need to be accessed are typically public because they're part of a well-designed API
// rather than being an internal detail of the class being accessed by classes located in the same package...
String thisIsValid = "becauseItsADeclaration";
public void test() {
System.out.println("Inside a method I can do whatever I want");
}
}