File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wrt to java Array programming

 
RaviNada Kiran
Ranch Hand
Posts: 528
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had always difficulties with Arrays .

int a[] = {1,2,3,4,5,6,7,8};

How can i copy the above array a[] data into a new array b[]

int b[] = new int[a.length];

Please help waiting for your responses
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Wont the for loop works well ? just iterate the length of array and grab each index values and assign it to new array
 
John de Michele
Rancher
Posts: 600
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will work, too:



John.
 
Balu Sadhasivam
Ranch Hand
Posts: 874
Android Java VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John de Michele wrote:This will work, too:



John.


Arrays.copyOf is available from Java 6.0.
 
Henry Wong
author
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Another option is to use the arraycopy() method of the java.lang.System class.

Henry
 
Campbell Ritchie
Sheriff
Pie
Posts: 47228
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another option is to use the clone() method of the array. It will work nicely for a plain simple 1-subscript array like that.By the way: write int[] a rather than int a[]. That asserts that the type of a is int[] not int.
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your time , But i dont want to do it any operation with the help of java.util.Arrays.

 
RaviNada Kiran
Ranch Hand
Posts: 528
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int a[] = {1,2,3};
int b[] = new int[a.length];

for(int i=0;i<a.length;i++)
{
a[i]= b[i];
}
System.out.println(b[2]);


i am getting 0 , can anybody please tell me whats the correct approach to copy data from one array to another??
 
Henry Wong
author
Marshal
Pie
Posts: 20823
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i am getting 0 , can anybody please tell me whats the correct approach to copy data from one array to another??


Generally, when copying a source to a target, it is a good idea to put the target on the left side of the assignment.

Henry
 
RaviNada Kiran
Ranch Hand
Posts: 528
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot Henry the code is working fine.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic