- 1
How to reassign a variable in Java
Let’s say I have this in python l =[1,3,2,4,5] sums = [] Restart = 0 For i in l: If i == 4: # restart sums.append(restart) restart = 0 else: restart += i How would you do a code like this in Java, my intent is to know how restart = 0 in the if statement will work. When I do that in Java it says the variable already exists Please help
3 Antworten
+ 3
int restart = 0; //restart is now 0
restart = 1; //reassign restart to 1
Is that what you want?
+ 2
Abdul Kader Lougue
If you declare a variable and you are declaring again then you will get error. So you don't need to redeclare same variable, you just need to reasign value.
For example:
int restart = 0;
So if you want to reasign a value to this variable then just do this:
restart = 1;
If you do ( int restart = 1 ) again then you will definitely get an error.
+ 1
Oh yeah i just realized that in my actual code, i want to reassign the variable to an empty list, so thats what giving me error.
CarrieForle and AJ