When I'm assigning values to arrays in methods, they become 0 when I try to work with them in other methods.
I'm also not that experienced with programming in java.
Here is my code:
public int[] pVectorCoinOne = new int[2];
public int[] pVectorCoinTwo = new int[2];
public int[] pVectorCoinThree = new int[2];
public int[] pVectorCoinFour = new int[2];
public int[] pVectorCoinFive = new int[2];
public int[] pVectorCoinSix = new int[2];
public void setPositionVectors(){
int[] pVectorCoinOne = {lblCoinImage1.getX(), lblCoinImage1.getY()};
int[] pVectorCoinTwo = {lblCoinImage2.getX(), lblCoinImage2.getY()};
int[] pVectorCoinThree = {lblCoinImage3.getX(), lblCoinImage3.getY()};
int[] pVectorCoinFour = {lblCoinImage4.getX(), lblCoinImage4.getY()};
int[] pVectorCoinFive = {lblCoinImage5.getX(), lblCoinImage5.getY()};
int[] pVectorCoinSix = {lblCoinImage6.getX(), lblCoinImage6.getY()};
}
public void printAllToOutput(){
setPositionVectors();
System.out.println(Arrays.toString(pVectorCoinOne));
System.out.println(Arrays.toString(pVectorCoinTwo));
System.out.println(Arrays.toString(pVectorCoinThree));
System.out.println(Arrays.toString(pVectorCoinFour));
System.out.println(Arrays.toString(pVectorCoinFive));
System.out.println(Arrays.toString(pVectorCoinSix));
}
Fix like this:
public void setPositionVectors(){
pVectorCoinOne[0] = lblCoinImage1.getX(); //example
}
It is all about scope - local vs global. And you cannot set the array directly like this. In java, you can set constants to array only during initialization.
like:
public int[] array = {1, 2};
When you write
public void setPositionVectors(){
int[] pVectorCoinOne = {lblCoinImage1.getX(), lblCoinImage1.getY()};
int[] pVectorCoinTwo = {lblCoinImage2.getX(), lblCoinImage2.getY()};
int[] pVectorCoinThree = {lblCoinImage3.getX(), lblCoinImage3.getY()};
int[] pVectorCoinFour = {lblCoinImage4.getX(), lblCoinImage4.getY()};
int[] pVectorCoinFive = {lblCoinImage5.getX(), lblCoinImage5.getY()};
int[] pVectorCoinSix = {lblCoinImage6.getX(), lblCoinImage6.getY()};
}
the method initializes new local variables with the same names inside its own rather than calling the ones you declared above. So the variables will disappear when the method ends, and nothing will be changed.
Related
This code generates unsorted array. My question is how do I clone the generated array so I can reuse the unsorted array in different sorting algorithms.
public class UnsortedData{
private int [] coreData;
private int maxArraySize;
private int currentArraySize;
public UnsortedData(int size){
this.maxArraySize = size;
this.coreData = new int[this.maxArraySize];
this.currentArraySize = 0;
}
public boolean addData(int data){
if (currentArraySize < maxArraySize)
{
coreData[currentArraySize] = data;
currentArraySize++;
return true;
}
return false;
}
public class dataSorting {
public static void main(String[] args) {
Random rand = new Random();
UnsortedData uD = new UnsortedData(1000000);
for (int x = 0; x < 50; x++) {
uD.addData(rand.nextInt(3000000));
}
}
Use build in methods from java.lang.Object to clone the original array and java.util.Arrays to sort the cloned array.
int[] arr = {6,9,4,5}; // original array
int[] arrCopy = arr.clone() // we created a copy of the array
Arrays.sort(arrCopy); // it sort the array that we cloned
References: Arrays Class , Class Object
Easiest way? Object.clone()
int[] arr = coreData.clone();
You can also use the Stream API or external libraries such as Apache Commons.
/* This is a question for an online test where the student writes a function whose answer is validated by my 'correctfunction' which is hidden to the student. I want to compare the results of the two functions in main method.
*/
import java.util.Arrays;
class SortArr
{
static int[] arr = new int[10];
public int[] sortin(int[] ans)
{
Arrays.sort(ans);
System.out.println(Arrays.toString(ans));
return ans;
}
public int[] correctfunction(int[] sol)
{
Arrays.sort(sol);
System.out.println(Arrays.toString(sol));
return sol;
}
public static void main(String[] args)
{
arr = new int[] {4,8,3,15,2,21,6,19,11,7};
SortArr ob=new SortArr();
ob.correctfunction(arr);
ob.sortin(arr);
if(Arrays.equals(ob.sol == ob.ans)) //non-static method //equals(Object) cannot be referenced from a static context
//variable ob of type SortArr: cannot find symbol
System.out.println("correct");
else
System.out.println("incorrect");
}
}
First Arrays.equals(parameter1, parameter2) it takes two parameter, and what you did is totally wrong. To fix that see below code
public static void main(String[] args)
{
arr = new int[] {4,8,3,15,2,21,6,19,11,7};
SortArr ob=new SortArr();
int[] newSol = ob.correctfunction(arr);
int[] newAns = ob.sortin(arr);
if(Arrays.equals(newSol, newAns))
System.out.println("correct");
else
System.out.println("incorrect");
}
The function returns an array. So when you call the method ,save the returned arrays in some array variable!
I have made the required changes in your code. Hope they work for you.
import java.util.Arrays;
class SortArr
{
int arr1[];
int arr2[];
static int[] arr = new int[10];
public int[] sortin(int[] ans)
{
Arrays.sort(ans);
System.out.println(Arrays.toString(ans));
return ans;
}
public int[] correctfunction(int[] sol)
{
Arrays.sort(sol);
System.out.println(Arrays.toString(sol));
return sol;
}
public static void main(String[] args)
{
SortArr s = new SortArr(); //make an object of your class
arr = new int[] {4,8,3,15,2,21,6,19,11,7};
SortArr ob=new SortArr();
s.arr1 = ob.correctfunction(arr); // save the returned array
s.arr2 =ob.sortin(arr); // save the returned array
if(s.arr1 == s.arr2)
System.out.print("correct");
else
System.out.print("incorrect");
}
}
If my method return an array how can I link for it?
private int[] osszesVizsgalata(int currentFord){
int[] truefalse = new int[2];
for(int i = 0; i<currentFord-1;i++){
if(this.ellenfelValaszai[i] == true){truefalse[1]++;}
else{truefalse[0]++;}
}
return truefalse;
}
This is my method and I cant call it that I can use both element of truefalse.
osszesVizsgalata(2)[0];
This is my try.
Declare an array in the client method and initialize it with the result of this method:
public void clientMethod() {
int currentFord = 2;
int[] foo = osszesVizsgalata(currentFord);
//code used for example purposes
System.out.println(foo[0]);
}
Here is my code:
class Myclass {
private static int[] array;
public static void main(String[] args) {
Myclass m = new Myclass();
for (int i = 0; i < 10; i++) {
m.array[i] = i;
System.out.println(m.array[i]);
}
}
public Myclass() {
int[] array = new int[10];
}
}
It throws a java.lang.nullPointerException when trying to do this:
m.array[i] = i;
Can anybody help me please?
You have declared a local variable array in your constructor, so you're not actually initializing the array declared in Myclass.
You'll want to refer directly to array in the constructor. Instead of
int[] array = new int[10];
Use this
array = new int[10];
Additionally, you've declared array static in the scope of your Myclass class.
private static int[] array;
You only have one instance of Myclass here, so it doesn't matter, but normally this would not be static, if you're initializing it in a constructor. You should remove static:
private int[] array;
In your constructor you are making your assignment to a local variable names array, not the static class variable also named array. This is a scope problem.
I'm also guessing that since you access array via m.array, you want a member variable and not a static one. Here's the fix
class Myclass {
private int[] array;
public static void main(String[] args) {
Myclass m = new Myclass();
for (int i = 0; i < 10; i++) {
m.array[i] = i;
System.out.println(m.array[i]);
}
}
public Myclass() {
rray = new int[10];
}
}
in MyClass() type this
this.array = new int [10];
instead of this
int[] array = new int[10];
Your code should be as below. In the constructor, instead of initializing the instance variable you created a new local variable and the instance variable was left uninitalized which caused the NullPointerException. Also the instance variable shouldn't be static.
class Myclass {
private int[] array;
public static void main(String[] args) {
Myclass m = new Myclass();
for (int i = 0; i < 10; i++) {
m.array[i] = i;
System.out.println(m.array[i]);
}
}
public Myclass() {
array = new int[10];
}
}
First, if you plan to use array as a field of m (i.e. m.array) don't declare it as static, but:
private int[] array;
Next thing you have to do is to initialize it. Best place to do that is in the constructor:
public MyClass() {
array= new int[10]; //just array = new int[10]; don't put int[] in front of the array, because the variable already exists as a field.
}
The rest of the code should work.
If I initialize an array in a Java method like:
final double[][] myArray = new double[r][c];
Will I be allowed to do this later in the method?
myArray[0] = new double[c];
Yes you can. For more on arrays http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html
I'll provide you an example of this:
public class Main {
public static void main(String[] args) {
final int[] finalArray = new int[5];
finalArray[0] = 10;
System.out.println(finalArray[0]);
finalArray[0] = 9001;
System.out.println(finalArray[0]);
finalArray = new int[5] //compile error!!!
}
}
This is because the final modifier will say that the reference to the array (the pointer) can't change, but the elements of the array (that could have another pointer) can change with no problem.
EDIT:
Another example with 2d array:
public class Main {
public static void main(String[] args) {
final int[][] array2d = new int[5][];
for(int i = 0; i < array2d.length;i++) {
array2d[i] = new int[6];
}
//the size of the rows can change with no problem.
array2d[0] = new int[8];
}
}