Java: Array Slices

14 11 2008

First off, let me start by saying that I don’t really like Java.  We’re working from that assumption, understand? 🙂 

That being said, I have to work in it every day at ekiwi while we develope web “scrapes” for clients.  Our screen-scraper software can utilize Interpreted Java (via Beanshell), Python, JavaScript, VBscript, Jscript, and probably a few more that I’m forgetting to mention.  However, to maintain a standard, we write our scraping scripts in Java.

Given the fact that I think Java is rediculously too verbose, I was happy to find a way to at least partially get the effect of PHP or Python array slices.  Here it goes. Since the JDK 1.2, you can use the following code.

The senario is that we have one array, and we want to cut it into 2 halves. Theoretically, you could just copy the first half, or the second half, or any segment in the middle, so long as you have indexes to the array.

// Interpreted Java
String[] myArray = new String[ 11 ];

// Get the sizes for the arrays.
int firstHalfSize = myArray.length / 2;
int secondHalfSize = myArray.length - firstHalfSize;

String[] firstHalf = new String[ firstHalfSize ];
String[] secondHalf = new String[ secondHalfSize ];

// Arguments are: sourceArray, sourceStartIndex, destinationArray, destinationStartIndex, numElementsToCopy
System.arraycopy( myArray, 0, firstHalf, 0, firstHalfSize );
System.arraycopy( myArray, firstHalfSize, secondHalf, 0, secondHalfSize );

The “System.arraycopy” method is very efficient; it’ll beat any loop you can write in your own code.  (This is precisely why forward-thinking sripting languages like Python build this functionality into arrays from the start with a syntax like “myArray[ startIndex : length ]” (the colon is literal).)

So what does this mean?  Use it.  Bring happiness to those people who wind up using Java by speeding up an overly-verbose interpreted language.

Advertisements

Actions

Information

2 responses

1 12 2009
Java vs. Perl

I’m surprised that for web scraping, you use Java of all languages. It’s indeed OVERLY verbose, and Perl would probably get the job done way easier.

2 12 2009
tonightslastsong

You are quite right, however Perl is, as I say, “write-only” code. Code gets read far more than written, and for an enterprise-level product, the owner of that company moved to Java for a better GUI-based builder of “scraping sessions”. He began his business in Perl, but we have to agree that Perl is probably better suited for individual jobs, rather than building a reusable framework and GUI!

Thanks for the comment 🙂 Much appreciated.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: