Results 1 to 4 of 4
Thread: Array as object or param?
- 02-08-2011, 12:58 PM #1
Member
- Join Date
- Feb 2011
- Posts
- 43
- Rep Power
- 0
Array as object or param?
Hi!
If I have an array of objects, lets say NULL Object in this example. Is it possible to call actions, or methods if you will, upon it using this syntax: objects.method().
I tried to populate the array using different methods. I couldn't get objestc.method() working. Instead I had to do it like this, passing the array as a param:
How should I go about this?Java Code:populate(arrayOfTypeObject); //usage of method "on" array.
Edit, I think I got it working if I looped through the array.
But I preferred a method iterating instead of iterating a method...Java Code:for(loop with index i) object[i].populate();
Edit2, comments of this please... :-)Last edited by überfuzz; 02-08-2011 at 01:07 PM. Reason: Comment
- 02-08-2011, 02:10 PM #2
So Maybe I not correct understand question.
No you can't do it. because compiler need not null reference on the object that invoke anything methods.Skype: petrarsentev
http://TrackStudio.com
- 02-08-2011, 08:51 PM #3
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,547
- Rep Power
- 11
Some languages provide a syntax that lets you take an array and do things to each of its elements. Something along the lines of:
arrayOfTypeObject.each(populate);
Java doesn't do this: arrays have very limited behaviour, and there is no type corresponding to the sort of thing populate would have to be.
As you have found you have to loop through the array, either using an index or with an enhanced for loop:
Java Code:for(SomeClass sc :arrayOfTypeObject) { sc.populate(); }
(I am assuming the array is declared to actually contain things that implement populate(). An array of Object is a poor choice of array because Object also has very little behaviour.)
--------------------------
As far as null is concerned, you generally have to keep in mind that two things can be null: the variable representing the array AND a variable or expression representing the object at a given index. You can't use an expression like arr[i] unless you are sure that arr is not null, and you can't use a expression like arr[i].populate() unless you are sure that arr[i] is not null.
- 02-08-2011, 11:06 PM #4
Similar Threads
-
Variable of an object in an array compared to an element of another array?
By asmodean in forum New To JavaReplies: 23Last Post: 09-07-2010, 08:12 PM -
How to use <s:param> tag
By aruna.hcu in forum Web FrameworksReplies: 7Last Post: 01-13-2010, 08:25 AM -
Parsing & handling of <object> and <param> tags
By Floxxx in forum AWT / SwingReplies: 1Last Post: 12-29-2009, 03:32 PM -
Javadoc - cannot compile @param tags
By jon80 in forum New To JavaReplies: 9Last Post: 05-14-2009, 08:57 AM -
jsp:param action
By Java Tip in forum Java TipReplies: 0Last Post: 12-24-2007, 10:03 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks