• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

Possible to make a class that acts like an Array? (like C#)

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I remember in C#, you can do something (can't remember exactly) that will make your own class act just like it's an Array - you could forexample make a Book class and then a static class called Books and then just say Books[3] to access the fourth Book in Books.

Is there a way to do that in Java?
 
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Considering you can't have a static class I would guess no, but I'm not sure the advantage of that over just having an array (maybe a static array) of type Book. Maybe you could elaberate more on what you are trying to do.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So, if I understand your question correctly, you want to be able to create a class (say BookArray), that you can use like this:

To answer your question, no you cannot do this. However, there are other ways to accomplish the same thing. If your question is more than acedemic interest, perhaps you can post an explanation of what you want to do and we can help you find a Java-like way to do it.

Layne
 
Sandy Shaffer
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well I'm trying to make a class to keep track of a non-predetermined number of radio stations and each radio station is represented by a Station object that contains Name, URL, and Keyword[].

I want to be able to easily parse an XML document containing a set of Station elements into a bunch of Station objects inside a StationArray object.
Then I want to be able to pick a Station at random and then call some of its methods.
But I also want the StationArray object to have some special methods of its own that I can call to perform some custom actions on the entire collection of Station objects.
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you want to call a method on all of the Station elements you will have to iterate over them. You would probably want to store them in something like an ArrayList. You could create a class that would take an Array or Collection of Stations and perform some method on them all.
 
Sandy Shaffer
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
oh, no I'm sorry I didn't make it quiet clear enough - I don't mean perform a method on each one of the Stations in StationArray. I mean I want the StationArray class to have some methods for itself, for example a Save() method that saves the StationArray to disk as an XML document.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just make a class that implements List and has the extra functionality you want
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I read somewhere that in OOP, you should prefer encapsulation over inheritence. Although, either way would work, I would suggest that you create a class with an ArrayList (or other Collection if that would be more appropriate). You can provide a getStation() method that returns an individual Station and someone else can then call any methods they want on that Station.

HTH

Layne Lund
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would agree that your best bet is to write a class called "StationList" which has a List encapsulated inside of it. Then write a few accessor methods like setStation, getStation, size, addStation and maybe removeStation. These could all operate on a class called "Station" which has whatever properties you want a station to have....

I tend to do this with most custom list applications, and I like it because you are writing the list accessor methods yourself, so you can control what goes in and what goes out. A BIG plug here is that you do the casting to Object inside these accessor methods, so all the "client" programmer sees is Station's going in and out.

You can also do things like check for null, so that null references cannot be added to the list.

Hope that helps.
 
Ranch Hand
Posts: 1923
Scala Postgres Database Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You may use a class with static methods, to save an array of stations:
 
Wanna see my flashlight? How about this tiny ad?
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic