0

I have a problem passing a multiple array to constructor. Can we actually do that or not?

public class First {
public String[] a;
public String[] b;
public First(String[] a, String[] b){
    this.a=a;
    this.b=b;
}
}

And the next code is where I'm using class First.

Scanner ss =  new Scanner(System.in); 
int x; 
System.out.print("How many lines? "); 
x = ss.nextInt();
for(int i=0; i<x; i++){
System.out.print("A: "); a[i]=ss.nextString();
System.out.print("B: "); b[i]=ss.nextString();
}
First ff= new First(a,b);

In NetBeans, there is no error, but I can't use it in another class.

I'd be thankful if you would help me.

3 Answers 3

1

There is nothing wrong with passing in multiple arrays to a Java constructor. However, you may want to consider making copies of the arrays that you are passing in (with Arrays.copyOf()). You should also seriously consider making your actual array data private or protected.

Sign up to request clarification or add additional context in comments.

Comments

0

The Question is not cleared. You cant use it on another class means. PLease clear your question.. But below code is working fine.. you can check it out.

public class MultipleArr {

String a[];
String b[];
public MultipleArr(String[] a, String[] b)
{
    this.a=a;
    this.b=b;
}

public static void main(String[] args) {

    String [] a={"1","2","3"};
    String [] b={"2","2","3"};

    MultipleArr arr=new MultipleArr(a,b);

    for(int i=0;i<arr.a.length;i++)
    {
        System.out.println(arr.a[i]);
    }

    for(int i=0;i<arr.b.length;i++)
    {
        System.out.println(arr.b[i]);
    }

}

}

1 Comment

I mean im gonna make the program input from user instead of we put the value from the first time. Scanner ss = new Scanner(System.in); int x; System.out.print("How many lines? "); x = ss.nextInt(); for(int i=0; i<x; i++){ System.out.print("A: "); a[i]=ss.nextString(); System.out.print("B: "); b[i]=ss.nextString(); } First ff= new First(a,b);
0

public class MultipleArr {

String a[];
String b[];
public MultipleArr(String[] a, String[] b)
{
    this.a=a;
    this.b=b;
}

public static void main(String[] args) {

    String [] a;
    String [] b;

    Scanner ss = new Scanner(System.in); 
    int x; 
    System.out.print("How many lines? "); 
    x = ss.nextInt();

    a=new String[x];
    System.out.print("A: ");
    for(int i=0; i<x; i++)
    { 

       a[i]=ss.next();
    }

    b=new String[x];
    System.out.print("B: ");
    for(int i=0; i<x; i++)
    { 

       b[i]=ss.next();
    }


    MultipleArr arr=new MultipleArr(a,b);

    for(int i=0;i<arr.a.length;i++)
    {
        System.out.println(arr.a[i]);
    }

    for(int i=0;i<arr.b.length;i++)
    {
        System.out.println(arr.b[i]);
    }

}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.