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:
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:
-----------------------------------------------------------------------------------------------------
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:
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=("user0@mycompany.com","user1@mycompany.com","user2@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.$EmailAddresses=$EmailAddresses+user3@mycompany.com"
$EmailAddresses=$EmailAddresses+"user4@mycompany.com"
$EmailAddresses=$EmailAddresses+"user5@mycompany.com"
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