-4
public class Test {

int[] a1 = {1, 3, 5, 7, 2};
int[] a2 = new int[a1.length + 10];


public static int[] Array(){

for(int i = 0; i < a1.length; i++){
a2[i] = a1[i];
} 
a1 = a2;



}


public static void main(String args[]){
  System.out.println(a1.toString);
 }
}

I am trying to create a method "Array" that will take in arrays a1 and a2 and add + 10 to the length of a1. My problem is that I don't know how to correctly name the method and how to call it in the main in order to print.

I have tried passing a1 and a2 in the constructor for Array but it doesn't work. I have also tried printing directly in the main() and it doesn't work either. What am I missing?

  • You need to make the variables and the method static. – gommb Oct 29 '17 at 22:39
  • 1
    You are missing quite a lot. The return type is incorrect `int[]` (not `int`). Also, that isn't how you print an array. And your method doesn't do what you think it does (at least I don't think it does). – Elliott Frisch Oct 29 '17 at 22:40
  • @ElliottFrisch "Resize" an array a1 by adding 10 more cells is what I'm doing. Changed the code but still incorrect.. – Robert Miller Oct 29 '17 at 22:46
  • Please don't post questions that say something "doesn't work", without explaining what the expected output is and what the actual behavior is. Are you getting an error from the compiler, wrong results, an exception when you run it, or what? – ajb Oct 29 '17 at 22:46
  • @ajb Now I am getting errors about a static method. The expected output should be the string of a1 – Robert Miller Oct 29 '17 at 22:50
  • Does the compiler say "Errors about a static method"? Or is the error message it gives you more descriptive? Please do not hide information from us. – ajb Oct 29 '17 at 22:51
  • @ajb "Test.java:9: error: non-static variable a1 cannot be referenced from a static context" – Robert Miller Oct 29 '17 at 22:52

2 Answers2

2

No idea what are you trying to do but in Java you can't change the size of array once it is declared but if you want to work with array with can change size then you should use arraylist not array and one more thing setting b = awould not work btw here is a code which swaps elements from one array to other but once again you can't change size of array in java: I return b back but you don't need to return an array back because an array variable(name of array) refers to a memory location.

public static int[] extendLength(int [] a, int [] b){
    for(int i = 0; i < a.length; i++){
     b[i] = a[i];
    } 
    return b;



    }

    public static void main(String args[]){
         int[] a1 = {1, 3, 5, 7, 2};
         int[] a2 = new int[a1.length + 10];
       int [] res = extendLength(a1,a2);
        for(int i = 0; i < a2.length;i++)
          System.out.println(a2[i]);
     }
Luai Ghunim
  • 976
  • 7
  • 14
1

Long story short, the way you have your program set up, your variables will also need to be static; otherwise, your static methods will not be able to access them.

Static fields and methods are essentially singular to the class; therefore without a director to which object contains the field, asking a static method to manipulate a non-static field is very ambiguous.

I'm seeing a number of other problems in this program as well; namely, you're treating toString as a field (it's a method), your array method is missing a return statement, and your alignment is all over the place. This probably isn't an appropriate question to bring to Stack Overflow; I would encourage you to look at a tutorial, like the one at Tutorials Point, before bringing your question here.

Michael Macha
  • 1,729
  • 1
  • 16
  • 25