This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Java in General and the fly likes CheckStyle Warning - User System.arrayCopy instead of looping through an array to copy elements Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "CheckStyle Warning - User System.arrayCopy instead of looping through an array to copy elements" Watch "CheckStyle Warning - User System.arrayCopy instead of looping through an array to copy elements" New topic
Author

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

Tiya Khambadkone
Ranch Hand

Joined: Sep 15, 2011
Posts: 52
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: 38519
    
  23
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: 3013
    
  10
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: 52
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: 7718
    
  20

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?
Articles by Winston can be found here
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3013
    
  10
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: CheckStyle Warning - User System.arrayCopy instead of looping through an array to copy elements