arrays print out method java -


i try write program displays values of array , composed of 2 classes.

one of these classes contains method uses system.out.print in loop:

public class methodsforarray{ int numbers[];      public void printoutarray(){         (int i=0; i<numbers.length; i++){         system.out.print(numbers[i]);         }     } } 

in other class method printoutarray() applied:

public class application1{ public static void main(string[]args){  methodsforarray myobject=new methodsforarray(); myobject.numbers[]={1,3,4}; myobject.printoutarray();    //here apply method      } } 

this way of doing works display strings or integers. why not work arrays? , how fix program? trying compile class application1, results in following error message:

application1.java:5: error: not statement myobject.numbers[]={1,3,4};                 ^ application1.java:5: error: ';' expected myobject.numbers[]={1,3,4};                   ^ application1.java:5: error: not statement myobject.numbers[]={1,3,4};                     ^ application1.java:5: error: ';' expected myobject.numbers[]={1,3,4};                      ^ 4 errors 

thanks.

there few things missed out.

1] should define class name such first letter should uppercase - hence methodsforarray

2] have declared int numbers in methodsforarray havent initialized/defined yet. hence whenever assign value should defining , assign value; in case have assigned anonymous array

myobject.numbers=new int[]{1,3,4}; 

please find working code sample below

public class mainclass{     public static void main(string[]args){          methodsforarray myobject=new methodsforarray();          myobject.numbers=new int[]{1,3,4};          myobject.printoutarray();    //here apply method      } }  class methodsforarray{     int numbers[];          public void printoutarray(){             (int i=0; i<numbers.length; i++){                 system.out.print(numbers[i]);             }         } } 

Comments

Popular posts from this blog

ios - MKAnnotationView layer is not of expected type: MKLayer -

ZeroMQ on Windows, with Qt Creator -

unity3d - Unity SceneManager.LoadScene quits application -