0

I'm making a method that takes 2 arrays and adds them together. (I'm a newbie and this is a school assignment, and I have learned that some people don't like to see school questions here. But I have done alot of the work already, I'm just stuck now.)

public static int[] Concat(int[] array1, int[] array2) //array1 = 1, 2, 3, 4, 5 array2 = 6, 7, 8, 9, 10.
    {
        int i = array1.Length + 1;
        int[] array3 = new int[array1.Length + array2.Length];
        Class1.CopyTo(array1, array3, 0);
        Class1.CopyTo(array2, array3, i);
        Class1.PrettyPrint(array3);
        return array3;
    }

In this method im referring to 2 other methods I did previously:

public static int[] CopyTo(int[] arr1, int[] arr2, int start)
    {

        for(int i = 0; i < arr1.GetLength(0); i++)
        {
            if (start <= i)
            {
                arr2[i] = arr1[i];
            }
        }
        return null;
    }

This one is for copying indexes of an array into another array. The start variable is the index where I will first start to copy.

public static int[] PrettyPrint(int[] intArray)
    {
        string result = string.Join(", ", intArray);
        Console.WriteLine(result);
        return null;
    }

And this prints out an array in a string.

The problem lies in the Concat method. It works for the first copy. Where I copy array1 into array3.

Class1.CopyTo(array1, array3, 0);

But for the second array, it adds nothing and all I get from typing it out as a string is:

1, 2, 3, 4, 5, 0, 0, 0, 0, 0.

I don't understand why only the first copy works.

Also a follow up question. In my main program tab I could no longer reference these methods with an instance of my class Class1 lab2 = new Class1();. But I had to call them by using Class1 instead of lab2 (which worked for my other methods). To be able to call a method inside another method in the same class I had to add "static" and by adding "static" I had to change how I call the method in my main program tab. I didn't really understand why I had to first add "static" and then change how I call the method. Why did I have to change the 2 methods that were called to static? And why couldn't I call those changed methods with the instance lab2?

Sorry for the long question!

kablash
  • 39
  • 1
  • 7
  • The reason your second call to `CopyTo` doesn't do anything is because you only copy if `start <= i` and that is never true. Step through your code in the debugger. – Dour High Arch Nov 15 '16 at 01:35
  • http://stackoverflow.com/questions/1547252/how-do-i-concatenate-two-arrays-in-c – Slai Nov 16 '16 at 00:01

2 Answers2

1

The reason you are not having the second array copied, is because CopyTo method checks and copies to arr2 only if i >= start, which it can’t happen when start is greater than arr1 length, which is the case for your second array array2.

You have to make 2 changes in order to make it work:

1: in Concat method, remove the + 1 to the int i = array1.Length + 1; (start paramenter for the second CopyTo). You don’t have any extra integers:

public static int[] Concat(int[] array1, int[] array2) //array1 = 1, 2, 3, 4, 5 array2 = 6, 7, 8, 9, 10.
{
    int i = array1.Length; // + 1;
    int[] array3 = new int[array1.Length + array2.Length];
    Class1.CopyTo(array1, array3, 0);
    Class1.CopyTo(array2, array3, i);
    Class1.PrettyPrint(array3);
    return array3;
}

2: in CopyTo, remove the if statement inside the loop and just add to the arr2 using the start to adjust and copy to the correct index of arr2 like this:

public static int[] CopyTo(int[] arr1, int[] arr2, int start)
{

    for (int i = 0; i < arr1.GetLength(0); i++)
    {
        arr2[start + i] = arr1[i];

        /*if (start <= i)
        {
            arr2[i] = arr1[i];
        }*/
    }
    return null;
}

Now you got the correct result array: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10

EDIT:

For your follow up question, you cannot call any methods directly to classes, unless these methods are static methods. You have to create and initialize an object of the class to call normal methods. Class1 is your class and lab2 is your object of the Class1 class. You can call normal methods to the object lab2, but you cannot call methods to its class Class1, unless these methods are defined static. For more information on static methods check these links:

Java: when to use static methods

Static Classes and Static Class Members (C# Programming Guide)

Community
  • 1
  • 1
Christos Lytras
  • 36,310
  • 4
  • 80
  • 113
0

You can also use two indexes one being the sourceIndex and the other being the targetIndex. This will greatly simplify things

Illustration:

public static int[] CopyTo(int[] arr1, int[] arr2, int startSource, int startTarget)
        {
            int index = startTarget;
            for (int i = startSource; i < arr1.GetLength(0); i++)
            {     
               arr2[index++] = arr1[i];   
            }
            return null;
        }

 public static int[] Concat(int[] array1, int[] array2) //array1 = 1, 2, 3, 4, 5 array2 = 6, 7, 8, 9, 10.
        {
            int i = array1.Length + 1;
            int[] array3 = new int[array1.Length + array2.Length];
            Class1.CopyTo(array1, array3, 0,0);
            Class1.CopyTo(array2, array3, 0, array1.Length);
            Class1.PrettyPrint(array3);
            return array3;
        }

        // Unchanged
        public static int[] PrettyPrint(int[] intArray)
        {
            string result = string.Join(", ", intArray);
            Console.WriteLine(result);
            return null;
        }

enter image description here

alainlompo
  • 4,414
  • 4
  • 32
  • 41