Copying an Array of int
You can first have a look back at lab 29 to understand why
int[] orig = { 1, 2, 3, 4, 5 };
int[] arrayCopyWrong = orig;
is not the appropriate way to copy an array.
Then, like we did in class:
- Write a snippet that creates a copy of
orig by declaring an array of size 5, and copying the values one by one.
- Change your snippet to makes it more generic: it should create an array of the same size as the size of
orig, and copy the values in it with a for loop.
- Then, write a static class with two methods:
- One that takes two array of integers as arguments, and copy the values of the first in the second. Try to make your method so that if the copy does not have the same size as the original, your program won’t crash.
- One that takes one array of integers as argument, and return a copy of it.
Once you are done with this, write two other copy methods taking only one argument: one for arrays of decimals, and one for arrays of strings. Can you give them the same name as for the one that takes arrays of integers as arguments? Why, or why not?