| Author |
CheckStyle Warning - User System.arrayCopy instead of looping through an array to copy elements
|
Tiya Khambadkone
Ranch Hand
Joined: Sep 15, 2011
Posts: 40
|
|
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
Joined: Oct 13, 2005
Posts: 32689
|
|
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
Joined: Mar 05, 2008
Posts: 2782
|
|
|
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
Joined: Sep 15, 2011
Posts: 40
|
|
|
can someone tell me the solution ? how to use System.arraycopy for the given piece of code ?
|
 |
Winston Gutkowski
Bartender
Joined: Mar 17, 2011
Posts: 4752
|
|
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
|
Isn't it funny how there's always time and money enough to do it WRONG?
|
 |
Mike Simmons
Ranch Hand
Joined: Mar 05, 2008
Posts: 2782
|
|
|
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.
|
 |
 |
|
|
subject: CheckStyle Warning - User System.arrayCopy instead of looping through an array to copy elements
|
|
|