+ Reply to Thread
Results 1 to 5 of 5

Thread: Tutorial, arraycopy

  1. #1
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Tutorial, arraycopy

    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.
    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);
    	
    	}
    }
    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)
    Now this should be simple to work on more advanced things. Lets look into the more advanced part.

    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
    	}
      }
    }
    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.
    Cheers !

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Tutorial, arraycopy

    Can you post the output of the two snippets above?

  4. #3
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Tutorial, arraycopy

    Quote Originally Posted by John View Post
    Can you post the output of the two snippets above?
    Sure, the output will look like this:
    Code:
    0
    1
    2
    3
    4
    95
    96
    97
    98
    99
    50
    51
    52
    53
    54
    15
    16
    17
    18
    19

  5. #4
    Join Date
    Jul 2006
    Location
    Amherst, New York, United States
    Posts
    6,277
    Blog Entries
    26
    Rep Power
    20

    Re: Tutorial, arraycopy

    Thank you, that makes the method a lot more clear.

  6. #5
    Join Date
    May 2008
    Location
    Hell
    Posts
    3,852
    Blog Entries
    4
    Rep Power
    49

    Re: Tutorial, arraycopy

    Quote Originally Posted by John View Post
    Thank you, that makes the method a lot more clear.
    No problem

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Help with a VB tutorial please.
    By brap in forum Visual Basic Programming
    Replies: 0
    Last Post: 09-15-2010, 09:58 AM
  2. PHP:Tutorial - Getting to know GD
    By John in forum PHP Tutorials
    Replies: 50
    Last Post: 08-20-2009, 07:38 PM
  3. PHP Tutorial
    By Xav in forum PHP Development
    Replies: 0
    Last Post: 04-09-2008, 01:13 PM
  4. Need Help With Tutorial
    By gszauer in forum Java Help
    Replies: 2
    Last Post: 02-22-2008, 10:06 AM
  5. Hello, New and need a tutorial.
    By Cageman in forum C and C++
    Replies: 4
    Last Post: 12-12-2007, 11:23 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts