Newbie Java array question

Status
You're currently viewing only Bagheera's posts. Click here to go back to viewing the entire thread.
Not open for further replies.

Bagheera

Ars Tribunus Angusticlavius
8,773
Subscriptor
I'm a CS student taking a 100-level class in Java. We're just learning about Java arrays this week. I posted this in the class conference, and I'd like to get some input from experienced programmers on Ars.

-----------------------------------------------------------------------------------------------------

I can't help but focus on the fact that Java arrays can't be dynamically resized. The "deep copy" method described in our book* seems entirely too complicated.

The only other programming language I know is Powershell. I'm told it handles arrays in a manner that similar to Python and other common scripting languages. In Powershell, arrays can be increased in size as you add more elements.

Say, for example, that you have an array of e-mail addresses. You could initialize an array with:
$EmailAddresses=("user0@mycompany.com","user1@mycompany.com","user2@mycompany.com")
Now we have an array with 3 elements. But what happens if we want to add more elements and increase the size of the array? Do we need a deep copy? Do we even have to worry about the array's memory allocation? No, we just enter the next elements:
$EmailAddresses=$EmailAddresses+user3@mycompany.com"

$EmailAddresses=$EmailAddresses+"user4@mycompany.com"

$EmailAddresses=$EmailAddresses+"user5@mycompany.com"
Now we have an array with 6 elements. In Java, we would need to do a deep copy for each of these three lines. If we're pulling thousands of e-mail addresses from a database, and we don't know exactly how many addresses there are, we have to set up a deep copy loop for every single address we add.

I imagine Powershell (and other languages) performs something like a deep copy when we add an element; we just don't have to explicitly program it every time. I'm certainly not saying Powershell is superior to Java. It just seems that Powershell makes arrays easier to handle. Can someone with more programming experience comment on this?

--------------------------------------------------------------------------

* The "Deep Copy" method was written as:

public static int[] deepCopy(int[] array1) {
int[] array2 = new int[array1.length];
for (int i = 0; i < array1.length; i++) {
array2 = array1;
} // end for

return array2;
} // end method


 

Bagheera

Ars Tribunus Angusticlavius
8,773
Subscriptor
Blacken00100":4f3t4v79 said:
If you need to resize an array in the normal course of programming, however, that should be something of a warning flags.

Really? What if you're querying multiple tables in a database, or grabbing event logs from multiple servers, or grabbing looped input from a user? In each case, you don't know how many elements (how many rows, events, or inputted items) you'll eventually get. Is that a warning sign or just typical programming?
 
Status
You're currently viewing only Bagheera's posts. Click here to go back to viewing the entire thread.
Not open for further replies.