Personal computing discussed

Moderators: renee, SecretSquirrel, just brew it!

 
Nitrodist
Grand Gerbil Poohbah
Topic Author
Posts: 3281
Joined: Wed Jul 19, 2006 1:51 am
Location: Minnesota

Java Array Element Immunitability

Thu Feb 11, 2010 9:25 pm

Is there a way to make the elements of an array immutable?

Say


final double int[] array = {1, 3};

double int[] newArray = {2, 48};

//This doesn't work
//array = newArray

//This works.
array[0] = 2;
array[1] = 48;

Image
 
wibeasley
Gerbil Elite
Posts: 952
Joined: Sat Mar 29, 2008 3:19 pm
Location: Norman OK

Re: Java Array Element Immunitability

Thu Feb 11, 2010 10:21 pm

In C++ you can use the const keyword; does Java have that?. In C#, you can't make the elements readonly, but you can make the pointer readonly to client classes if you expose the array as a readonly property. This fits your scenario. Does Java has a similar convention when exposing reference types as properties? Is it ok if code within the class can change the address, as long as code outside your class can't change it?
 
Zoomastigophora
Gerbil Elite
Posts: 667
Joined: Tue Nov 11, 2008 7:10 pm

Re: Java Array Element Immunitability

Fri Feb 12, 2010 2:02 am

No. What you can do is wrap the array in another class and then provide only a getter for the values. Why do you want to prevent the elements in the array from being changed?

wibeasley: Java's equivalent of const is final, although it's not usable in all the places const can be used in C++. Java also doesn't have the concept of properties; if you need to access a private variable inside a class, the class needs to provide a function for you to call to get what you need.
 
arsenhazzard
Gerbil First Class
Posts: 169
Joined: Thu Oct 18, 2007 4:30 pm

Re: Java Array Element Immunitability

Fri Feb 12, 2010 2:24 am

Zoomastigophora wrote:
No. What you can do is wrap the array in another class and then provide only a getter for the values. Why do you want to prevent the elements in the array from being changed?

wibeasley: Java's equivalent of const is final, although it's not usable in all the places const can be used in C++. Java also doesn't have the concept of properties; if you need to access a private variable inside a class, the class needs to provide a function for you to call to get what you need.


Check out Collections.unmodifiableList if you can use Lists (or a Set, Map, etc) instead of an array, which is a handy method of doing what Zoomastigophora suggests
 
Nitrodist
Grand Gerbil Poohbah
Topic Author
Posts: 3281
Joined: Wed Jul 19, 2006 1:51 am
Location: Minnesota

Re: Java Array Element Immunitability

Fri Feb 12, 2010 1:33 pm

Zoomastigophora wrote:
No. What you can do is wrap the array in another class and then provide only a getter for the values. Why do you want to prevent the elements in the array from being changed?


Because my grade on an assignment almost was dropped by 10% because I didn't use the 'final' keyword on an array (which I then demonstrated that you could still change the individual elements within the array). :o Then I was curious on how to actually make it so that the array's elements (or what we're going to do now is an object) would be immutable.

The alternative for constants is individual values such as :

final double STUDENT = 395.00;
final double BUSINESS = 595.00;
final double COMPLIMENTARY = 0.00;


And having to correspond those values to a list such as a JComboBox, which would be fairly easy to use as the list gets larger. I wouldn't have to put a case statement for each element or an if statement for each named constant.

for example, the above named constants would be as such:

double cost = 0.0;
int selected = list.getSelectedIndex();

if (selected == 0)
  cost += STUDENT;
else if (selected == 1)
  cost += BUSINESS;
else if (selected == 2)
  cost += COMPLIMENTARY;


or with a case statement:


switch(selected)
{
  case 1 : result = BUSINESS; break;
  case 2 : result = STUDENT; break;
  case 3 : result = COMPLIMENTARY; break;
}



As the list increases for options, so does that series of if statements or the case statement's length.

As you can see, the array is an easier structure to control and I would only have to add it to the array & the corresponding list in question for each additional element instead of adding it to the list, then as a named constant and then as an additional case or if statement.

cost += array[list.getSelectedIndex()];
Image
 
SNM
Emperor Gerbilius I
Posts: 6209
Joined: Fri Dec 30, 2005 10:37 am

Re: Java Array Element Immunitability

Fri Feb 12, 2010 1:45 pm

Use the named constants, then put those in the array with appropriately-matched named indices. If you use "magic numbers" without associated labels your code quickly becomes completely indecipherable -- and what happens if the "student" values change or you add in another type that goes in the middle of the ones you have? You then need to update every occurrence of that number in the code base!
Core i7 920, 3x2GB Corsair DDR3 1600, 80GB X25-M, 1TB WD Caviar Black, MSI X58 Pro-E, Radeon 4890, Cooler Master iGreen 600, Antec P183, opticals
 
wibeasley
Gerbil Elite
Posts: 952
Joined: Sat Mar 29, 2008 3:19 pm
Location: Norman OK

Re: Java Array Element Immunitability

Fri Feb 12, 2010 2:01 pm

...or enums instead of magic numbers.
 
Zoomastigophora
Gerbil Elite
Posts: 667
Joined: Tue Nov 11, 2008 7:10 pm

Re: Java Array Element Immunitability

Fri Feb 12, 2010 2:46 pm

Oh god, Java Swing. Raaaaaaaaaggggeeeeee :evil:.

Anyway, as mentioned before, you can't make individual elements in an array immutable so I hope you didn't get penalized for your grader's incompetency. Since you seem to be summing values based on user selections, I would either use the DefaultComboBoxModel or create a HashMap keyed on the possible strings returned by the JComboBox.
 
wibeasley
Gerbil Elite
Posts: 952
Joined: Sat Mar 29, 2008 3:19 pm
Location: Norman OK

Re: Java Array Element Immunitability

Fri Feb 12, 2010 3:10 pm

Is there any chance your grader wanted you to return a clone/copy of the array? It's not techinically making the elements immutable, but at least any changes by the client can't affect the values of the class being called.

Who is online

Users browsing this forum: No registered users and 1 guest
GZIP: On