Results 1 to 13 of 13

Thread: Array

  1. #1
    JAVA03 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default Array

    hi
    am not good enough in array so please some body help me in giving lots of example or link or book so that i can understand it and move to next step of java .

  2. #2
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by JAVA03 View Post
    hi
    am not good enough in array so please some body help me in giving lots of example or link or book so that i can understand it and move to next step of java .
    Have you studied Sun's/Oracle's Java Tutorials?

    kind regards,

    Jos

  3. #3
    JAVA03 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    no i haven't thanks.

  4. #4
    JAVA03 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    this is really a big good book for me .
    thanks.

  5. #5
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by JAVA03 View Post
    this is really a big good book for me .
    thanks.
    You're welcome; have fun studying it; when you have finished it all you are a better Java programmer ;-)

    kind regards,

    Jos

  6. #6
    JAVA03 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default Array Copying

    class ArCopy
    {
    public static void main(String[] args)
    {
    String copyfrom[]={"Hello", "this" , "is " ,"a", " copy", "example"};
    String copyto[]=new String[7];
    System.arraycopy(copyfrom,0,copyto,0,6);
    System.out.println(copyto);
    }
    }

    please check it out why it is giving me eror while rum time

  7. #7
    maknib is offline Member
    Join Date
    Nov 2010
    Posts
    90
    Rep Power
    0

    Default

    Quote Originally Posted by JAVA03 View Post
    class ArCopy
    {
    public static void main(String[] args)
    {
    String copyfrom[]={"Hello", "this" , "is " ,"a", " copy", "example"};
    String copyto[]=new String[7];
    System.arraycopy(copyfrom,0,copyto,0,6);
    System.out.println(copyto);
    }
    }

    please check it out why it is giving me eror while rum time
    hey mate

    i've always had trouble with arrays too. i haven't seen arrayCopy before but im going to guess a few things..

    1. i thought it was..
    String[] copyfrom = {.....};

    2. you are copying from 0 to 6. your array has 6 elements hello, this, is, a, copy, array. however arrays start at 0. so if 0 to 6 is counting the keys you only have 0 -> 5 not 0 -> 6 keys.


    however i just checked it.. copy pasted your code and it runs fine..

    but try this


    Java Code:
    public class test {
    
    	public static void main(String[] args)
    	{
    		String copyfrom[]={"Hello", "this" , "is" ,"a", "copy", "example"};
    		String copyto[]=new String[7];
    		System.arraycopy(copyfrom,0,copyto,0,6);
    		for(int i = 0; i < copyfrom.length; i++){
    		System.out.print(copyto[i] + " ");
    		}
    	}
    }
    Last edited by maknib; 11-03-2010 at 11:26 AM.

  8. #8
    JAVA03 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    but it should copy string copyfrom to copyto .why it is not doing in my code so?
    i did urs it is working.thanks
    let me try again.

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by JAVA03 View Post
    class ArCopy
    {
    public static void main(String[] args)
    {
    String copyfrom[]={"Hello", "this" , "is " ,"a", " copy", "example"};
    String copyto[]=new String[7];
    System.arraycopy(copyfrom,0,copyto,0,6);
    System.out.println(copyto);
    }
    }

    please check it out why it is giving me eror while rum time
    It would be nice if we'd know what the error was ...

    kind regards,

    Jos

  10. #10
    JAVA03 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    its a run time error it is giving

    [Ljava.lang.String;@3e25a5

    whats this error is and what mistake am doing?
    please clear my doubt and mistake

  11. #11
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by JAVA03 View Post
    its a run time error it is giving

    [Ljava.lang.String;@3e25a5

    whats this error is and what mistake am doing?
    please clear my doubt and mistake
    That's not an error; that's the way arrays present their String representation. Either print every element of the array yourself or use the Arrays class, it has a method for that (read the API documentation for that class).

    kind regards,

    Jos

  12. #12
    JAVA03 is offline Member
    Join Date
    Nov 2010
    Posts
    15
    Rep Power
    0

    Default

    class AR1
    {
    public static void main(String[] args)
    {
    String copyfrom[]={"Hello", "this" , "is " ,"a", " copy", "example"};
    String copyto[]=new String[7];
    System.arraycopy(copyfrom,0,copyto,0,6);
    System.out.println("the copi String at 0 : " + copyto[0]);
    System.out.println("the copi String at 0 : " + copyto[1]);
    System.out.println("the copi String at 0 : " + copyto[2]);
    System.out.println("the copi String at 0 : " + copyto[3]);
    System.out.println("the copi String at 0 : " + copyto[4]);
    System.out.println("the copi String at 0 : " + copyto[5]);

    }
    }

    and the out put is:
    the copi String at 0 : Hello
    the copi String at 1 : this
    the copi String at 2 : is
    the copi String at 3 : a
    the copi String at 4 : copy
    the copi String at 5 : example


    thanks you sir
    Any other way that i can do , like do to by various methods so that i can learn it .

  13. #13
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,663
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by JAVA03 View Post
    thanks you sir
    Any other way that i can do , like do to by various methods so that i can learn it .
    You're welcome of course; a few remarks: a copy of 6 (six) elements is enough, you don't need 7 elements. Why don't you use a loop when you want to print the array? i.e.

    Java Code:
    for (int i= 0; i < copyto.length; i++)
      System.out.println("the copi String at "+i+" : " + copyto[i]);
    Also read the API documentation for the Arrays class; it can create a copy of your array in one method call.

    kind regards,

    Jos

Similar Threads

  1. Replies: 23
    Last Post: 09-07-2010, 09:12 PM
  2. Replies: 2
    Last Post: 09-06-2010, 02:03 AM
  3. create a 2d char array from a 1D string array
    By jschmall12 in forum New To Java
    Replies: 1
    Last Post: 04-27-2010, 10:01 PM
  4. Convert Char Array to String Array
    By Mayur in forum New To Java
    Replies: 8
    Last Post: 10-12-2009, 12:41 PM
  5. Replies: 1
    Last Post: 03-31-2009, 07:40 AM

Posting Permissions

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