Nice to meet you C.C, am here again with a new tutorial. In this tutorial we going to learn how to use arraycopy
Let's first start with the code.
Okay so here is how it works. We first of need two arrays and have declared a value for them.(Doesn't matter how many you want etc blablabla). Now System.arraycopy is a "tool" would I say, to copy index numbers from an array to replace it in another array list.As for arraycopy the "path way looks like this) arraycopy(source,position of source(index number),destination,postition of destination(index number),length of the whole copy)Code:public class arraytest2 { public static void main(String [] arg) { char [] a = {'a','b','c'}; char [] b = {'d','e','f'}; System.arraycopy(a,1,b,1,2); System.out.print(b); } }
Now this should be simple to work on more advanced things. Lets look into the more advanced part.
So we saw the advantage of arraycopy, instead of doing the "all the same part" thing we could just do an arraycopy, to change values of the arrays we want.Code:public class arraytest3 { public static void main(String [] arg) { int [] a = new int[100];//here we have our first array with max index number 100(0-99) int [] b = new int[20];//here we have our second array with max index number 20(0-19) for(int i=0; i<100; i++) {//I made a for loop to help out myself the time to not type in every index numbers value a[i]=i;//declare the for loop is equal to the index numbers of array a } for(int i=0; i<20; i++) {//same as array a b[i]=i;//same as array a b[10] = a[50];//This part is a semi "array copy method I made for fun" b[11] = a[51];//Just to see the real function in an array b[12] = a[52];//all the same b[13] = a[53];//all the same b[14] = a[54];//all the same } for(int i=0; i<b.length; i++) {//last loop for the day System.arraycopy(a,95,b,5,5);//we create the new array list System.out.println(b[i]);//printing it out } } }
Cheers !
Can you post the output of the two snippets above?
Thank you, that makes the method a lot more clear.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks