im facing a problem with a part of a program.. if i have a program say..
class A { int x; String y[]; A(int x,String y[]) { this.x=x; for(int i=0;i<x;i++) { this.y=y; } } }
public class B { public static void main(String args[]) { int a; //get value of a
String b[]=new String[a]; //use for loop and get values in b[] A myA=new A(a,b); } }
now i know im going wrong somewhere cuz once my program reaches the constructor part, an exception is thrown..but i need to pass this array as a parameter as well!!..how do i rectify this??
class A{ int x; String y[] ; public A(int x,String y[]){ this.x=x; this.y = new String[x]; for(int i=0;i<x;i++){ this.y=y; } } }
public class B{ public static void main(String args[]){ int a=2; //get value of a
String b[]=new String[a]; //use for loop and get values in b[] try{ A myA=new A(a,b); }catch(Exception e){ e.printStackTrace(); } System.out.println("Done"); } }