• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CheckStyle Warning - User System.arrayCopy instead of looping through an array to copy elements

 
Tiya Khambadkone
Ranch Hand
Posts: 109
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I get a warning - 'User System.arrayCopy instead of looping through an array to copy elements'
for the following piece of code :

String[] a = {"a","b","c","d","e","f","g","h","i","j","k","l"};
String[] b = new String[11];
int idx = 0;
for(int i=1; i < a.length; i++){
b[idx++] = a[i-1] + "." + a[i];
}

Does anyone know how to fix it ?
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Stop using the check style tool.
Use System.arraycopy, which is specially optimised and may be faster than your loop. That is actually a good suggestion from the check style tool.

But remember, check style warnings are only suggestions, which you are not obliged to follow.
By the way, it’s not arrayCopy. Please always use copy-and-paste as suggested here, which is more accurate (and faster) than copying error messages by hand.
 
Mike Simmons
Ranch Hand
Posts: 3041
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suspect that "User System.arrayCopy" should say "Use System.arraycopy"- it's an instruction, telling you to use the method System.arraycopy() to copy the array elements.
 
Tiya Khambadkone
Ranch Hand
Posts: 109
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
can someone tell me the solution ? how to use System.arraycopy for the given piece of code ?
 
Winston Gutkowski
Bartender
Pie
Posts: 10281
60
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tiya Khambadkone wrote:can someone tell me the solution ? how to use System.arraycopy for the given piece of code ?

Here you go.

Read the documentation and try it. If you still have problems, come back with what you tried and explain the problems you're having. This site is NotACodeMill (←click).

Winston
 
Mike Simmons
Ranch Hand
Posts: 3041
10
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Actually, now that I look at what's being "copied", I don't think it's possible - you're changing the values as they're "copied", so they aren't really copied, so System.arraycopy() won't work. I suggest disabling the inspection, at least for this particular bit of code, or just ignoring it.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic