Goldengirl
Goldengirl

Reputation: 967

How to Concatenate arrays from a different class in Java?

I am a beginner at Java. I have created a main program with the main class . This is called Main.java. THis main class contains a set of arrays. These arrays are imported into the second class called the Test.java. Now I am looking for a method to concatenate all the arrays at the end of this second class. My code looks as shown below:

 import java.util.Arrays;
 public class Main{

      public enum State{A,D,H};

      Test[] tests = new Test[]{
               new Test(new State[]{State.A, State.H, State.A, State.H}),
               new Test(new State[]{State.A, State.H, State.A, State.D}),
               new Test(new State[]{State.H, State.D, State.A, State.A})
       };

The calss Test.java looks like this :

    import java.util.Arrays;
    public class Test{

    public static Main.State[] in;
    public static Main.State[] in_state= new Main.State[4];
    public static String data_in;

    Test(Main.State[] in){
        this in = in;
        this.in_state=in_state;

    for( int i=0; i<in.length; i++){
        in_state[i]=in[i];
        data_in =java.util.Arrays.toString(in_state);
        data_in = data_in.replaceAll(",", "");
        data_in = data_in.replaceAll(" ","");}
   System.out.println( "The input arrays are" +data_in) ;

Now the output I get looks like this :

    The input arrays are[AHAH]
    The input arrays are[AHAD]
    The input arrays are[HDAA]

Instead of this I want to get it as AHAHAHADHDAA. I tried using the ArrayUtils.addAll function but the program stops executing abruptly. Could somebody please help me out. Thank you in advance.

Upvotes: 2

Views: 115

Answers (1)

Anonymous Coward
Anonymous Coward

Reputation: 3200

You are trying to do too many things in the same place, the constructor for class Test.

Leave the assignment of initial state to the constructor. Put the merging code in a separate method and put the code to convert to String also in a separate method.

Also, members of Test should not be static, otherwise the get mixed up.

This would be the corrected code following those advices :

Main.java

public class Main
{

    public static enum State
    {

        A, D, H
    };

    public static void main(String[] args)
    {
        Test[] tests = new Test[]
        {
            new Test(new State[]
            {
                State.A, State.H, State.A, State.H
            }),
            new Test(new State[]
            {
                State.A, State.H, State.A, State.D
            }),
            new Test(new State[]
            {
                State.H, State.D, State.A, State.A
            })
        };
        Test testMerged = Test.merge(tests);
        System.out.println("The input arrays are" + testMerged);
    }
}

Test.java

public class Test
{

    static Main.State[] in;

    public static Test merge(Test[] tests)
    {
        int size = calculateSize( tests );
        Main.State[] state = new Main.State[size];
        int i = 0;
        for ( Test test : tests )
        {
            for ( Main.State s : test.in )
                state[i++] = s;
        }
        return new Test( state );
    }

    private static int calculateSize(Test[] tests)
    {
        int result = 0;
        for ( Test test : tests )
        {
            for ( Main.State s : test.in )
                ++result;
        }
        return result;
    }

    Test(Main.State[] in)
    {
        this.in = in;
    }

    @Override
    public String toString()
    {
        String result =java.util.Arrays.toString(in);
        result = result.replaceAll(",", "");
        result = result.replaceAll(" ","");
        return result;
    }
}

Upvotes: 1

Related Questions