Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 19 of 19

Thread: Details about 'CopyTo' of Arrays in Java

  1. #1
    Member Fendaril's Avatar
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    7
    Thanked 6 Times in 5 Posts

    Default Details about 'CopyTo' of Arrays in Java

    I dont want to make this a lengthy post so I just started to learn JAVA a few days ago so that I could have a better understanding of programming when I get out of highschool.

    I was reading up on the JAVA tutorials when I neared the end of the array section. I understand everything else except at the very end when arrays are copied.

    Here is the code from the tutorial

    class ArrayCopyDemo {
    public static void main(String[] args) {
    char[] copyFrom = { 'd', 'e', 'c', 'a', 'f', 'f', 'e',
                 'i', 'n', 'a', 't', 'e', 'd' };
    char[] copyTo = new char[7];
     
    System.arraycopy(copyFrom, 2, copyTo, 0, 7);
    System.out.println(new String(copyTo));
    }
    }

    I get the first part when "CopyFrom" is declared with a bunch of characters but I was thrown off of the declaration of 'CopyTo'. please help me out. I tried searching on google but It just turns up with the syntax(which i dont get).


  2. #2
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: New to Java/struggling with arrays

    Hello Fendaril,

    Welcome to the Java Programming Forums

    copyFrom and copyTo are actually just variable names. These are unique names you declare to your variables in order to identify them later on.

    In this case you have 2 char variables called copyFrom and CopyTo but you could easily have something like

    String myName = "John";

    There you have created a String variable called myName containing the String 'John'

    The code:

    char[] copyTo = new char[7];

    Declares an char variable array which will hold 7 characters.

    The System.arraycopy basically prints out the array data from position 2 through to 7.

    These char array variables could be called anything you want. They are not actual java code. I hope you understand what I mean..
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  3. #3
    Member Fendaril's Avatar
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    7
    Thanked 6 Times in 5 Posts

    Default Re: New to Java/struggling with arrays

    Thanks for your help JAVApf. Do you know how I can increase my code time intuitiveness. I practice alot but my mind forgets alot and I cant structure code correctly all the time. thanks in advance.

  4. #4
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: New to Java/struggling with arrays

    Hey Fendaril,

    Its my pleasure.

    I know the feeling.. Its quite daunting when first starting to write Java code. It seems like there is soooo much to learn.

    What I would suggest is that you start your own code library. Create a folder somewhere on your computer and create .txt files containing different code. For example, I would have one containing code for reading files, reading input from the console, an array example etc. Name each file approperatly and then when you are writing new code you can check your library as a reference.

    The trick really is to practice. The more real world applications you can write the better. It takes time but eventually things start to click into place and it becomes alot easier.

    You will never stop learning. Ive been writing Java code for quite a few years now but im still learning every time I code.

    There is an official Java exam called the SCJP "Sun Certified Java Programmer" Take a look at some of the ebooks we have on this site. Because they are designed to help you pass the exam, they cover every aspect of Java and will help you to understand areas you are unsure about.

    http://www.javaprogrammingforums.com...-new-post.html

    Remember, google is your friend. You will find nearly all your answers there and posting in forums like this one will also help you on your way
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  5. #5
    Member Fendaril's Avatar
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    7
    Thanked 6 Times in 5 Posts

    Red face Re: New to Java/struggling with arrays

    That e-book on JAVA really cleared up Arrays for me. To tell you the truth I thought that all arrays had to be named "anArrayOfString;". I had no clue that I could name them what I want and initialize them on the same line. Goes to show how easy it is to misread JAVA sun's tutorials.

    I found this cool datatype called "Object" that can hold anything!!!!!

    I can even create arrays with it!!!

    What is the catch. Will is affect the speed of my program by alot?
    Last edited by Fendaril; November 4th, 2008 at 08:34 AM.

  6. #6
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: New to Java/struggling with arrays

    I'm glad that ebook has helped you. There is lots of useful information in there!

    Check this link out and read about what an Object is.

    What Is an Object? (The Java™ Tutorials > Learning the Java Language > Object-Oriented Programming Concepts)

    This is the very fundamentals of object oriented programming.

    Welcome to the world of Java
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  7. #7
    Member Fendaril's Avatar
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    7
    Thanked 6 Times in 5 Posts

    Default Re: New to Java/struggling with arrays

    ah you seemed to have misread me.

    I meant this:

    object test= 1234;(Is this more efficient then declaring an int?)

  8. #8
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: New to Java/struggling with arrays

    What is the catch. Will is affect the speed of my program by alot?
    Java is powerful. You would need some really heavy code to slow down your program by a noticable amount. You do not need to worry too much.

    Quote Originally Posted by Fendaril View Post

    I meant this:

    object test= 1234;(Is this more efficient then declaring an int?)
    You must use the correct variable for whatever type of data you are trying to hold. String, int, char, byte, double, etc.

    This makes it easy to identify exactly what you are doing and makes for good programming.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  9. #9
    Member Fendaril's Avatar
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    7
    Thanked 6 Times in 5 Posts

    Default Re: New to Java/struggling with arrays

    Thanks. Now one more question.SOrry for all of them. What are good places to do excercises?

  10. #10
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: New to Java/struggling with arrays

    Quote Originally Posted by Fendaril View Post
    Thanks. Now one more question.SOrry for all of them. What are good places to do excercises?
    Its no problem Fendaril... Thats what we are here for

    How do you mean by exercises? Are you looking for inspiration in what to code or are you looking for step by step instructions you can follow?

    If your looking for a few simple ideas then im sure I can chuck a few your way.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  11. #11
    Member Fendaril's Avatar
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    7
    Thanked 6 Times in 5 Posts

    Default Re: New to Java/struggling with arrays

    few ideas. Its one thing to know what the code does but a whole different issue of actually coding from scratch. I figured this is the best place to get ideas.

  12. #12
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: New to Java/struggling with arrays

    Its good to write code that will help you learn different aspects of Java.

    Do you feel you understand arrays enough now?

    How are you with the different loops? for loop, while loop etc?

    You could try to incorporate the use of an array and a for loop into one program.

    Create an array of Names and use a for loop to print them all to the console.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  13. #13
    Member Fendaril's Avatar
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    7
    Thanked 6 Times in 5 Posts

    Default Re: New to Java/struggling with arrays

    public class Test1{
    	public static void main(String[] args){
    		String[] names={"bob","jill","frank","stewart","charlie"};
    		for(String item: names){
    			System.out.println("This name in the array is " + item);
    		}
    	}
    }

    Relating to your other question yes I understand the basic loops and array concepts now. The problem is I looked at the candidate program that was very recently posted here and saw very clever use of loops and arrays. That is what I am aiming toward.

    I guess it will come with time.

  14. #14
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: New to Java/struggling with arrays

    ah yeah I can see why you would think this code is more complicated when it actually isnt.

    This is still a String array and for loop printing its content.

    Array values can be stored like this:

    String[] names={"bob","jill","frank","stewart","charlie"};

    This is the same as:

    String[] name = new String[5]
     
    name[0] = "bob";
    name[1] = "jill";
    name[2] = "frank";
    name[3] = "stewart";
    name[4] = "charlie";

    I think this concept first came into Java in version 4/5. They both work the same, its just down to personal preference to which one you use.

    for(String item: names){
                System.out.println("This name in the array is " + item);
            }

    The basic for loop was extended in Java 5 to make iteration over arrays and other collections more convenient. This newer for statement is called the enhanced for or for-each loop. Again its down to personal preference which one you use but keeping with the times makes it more readable.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  15. #15
    Member Fendaril's Avatar
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    7
    Thanked 6 Times in 5 Posts

    Default Re: New to Java/struggling with arrays

    so the reason I thought the candidate program was hard was because of the fact that he used a different method of array?

    would it be more beneficial to create a standard for loop and have it iterate over the length while I print the arrays?
    Last edited by Fendaril; November 7th, 2008 at 07:49 AM.

  16. #16
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: New to Java/struggling with arrays

    Yeah the reason it looked hard was because they used a different method of array.

    As far as im aware, there is no difference in performance between the for and for-each loop. But if you want to print the content of an array I would get used to learning the for-each loop as its more convenient and no doubt you will encounter it alot in the future.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  17. #17
    Member Fendaril's Avatar
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    7
    Thanked 6 Times in 5 Posts

    Default Re: New to Java/struggling with arrays

    Yes the easy part is over.

    Classes and objects seem to go through one ear and out the other...

  18. #18
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 287 Times in 225 Posts
    Blog Entries
    4

    Default Re: New to Java/struggling with arrays

    Quote Originally Posted by Fendaril View Post
    Yes the easy part is over.

    Classes and objects seem to go through one ear and out the other...
    It will take time to sink in. Classes and Objects are the base of Object Oriented Programming.

    Spend some time to read through the Sun Java tutorials about Classes and Objects. Even if you have read it 5 times before, read it again.

    Lesson: Classes and Objects (The Java™ Tutorials > Learning the Java Language)

    You need to keep reading even if you don't understand. Each time it will gradually sink in more and more. Try to write code that will help you to understand. For example, try to pass a variable between 2 different classes..

    I remember it took me a while to get my head around OOP too. The key really is just to keep writing code.
    Please use [highlight=Java] code [/highlight] tags when posting your code.
    Forum Tip: Add to peoples reputation by clicking the button on their useful posts.

    Looking for a Java job? Visit - Java Programming Careers

  19. #19
    Member Fendaril's Avatar
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    7
    Thanked 6 Times in 5 Posts

    Default Re: New to Java/struggling with arrays

    I havent done anything related to java in 2 days. I got so stressed out and just played games. I feel so incomplete so I am going to do something worthwhile.


    Ok so I think I came upon a good project to start out with. I found out how to pass variables through different methods.

    What if I passed variables through methods like 'addition(i,j)' while taking input from the user. It does require me to jump a head a little bit but a calculator is something that is very easy to make. I could use the beautiful if-else ladder. The process I have to take to make it is a bit harder then in c++. Not comparing or anything.
    Last edited by Fendaril; November 13th, 2008 at 07:37 AM.

Similar Threads

  1. How to Sort an Array using the java.util.Arrays class
    By JavaPF in forum Java SE API Tutorials
    Replies: 2
    Last Post: May 17th, 2014, 01:16 AM
  2. Replies: 1
    Last Post: December 22nd, 2011, 08:55 AM
  3. Elegant and short way to implement my program on 2d Array
    By TheForumLord in forum Collections and Generics
    Replies: 1
    Last Post: December 8th, 2008, 03:56 PM
  4. Java error in using Arrays
    By AnithaBabu1 in forum Collections and Generics
    Replies: 4
    Last Post: November 4th, 2008, 06:50 AM