Using an array of custom objects - java

Let's say I've created an array of objects SpecialResource via
ArrayList<SpecialResource> masterRes = new ArrayList<SpecialResource>();
masterRes.add(0, new SpecialResource(3,5,0,"Foo Bar"));
.........etc etc many more adds...
Let's say SpecialResource has a method calledgetMax()
How would I reference the getMax method of array index 0? Every permutation I've guessed at is giving syntax errors. masterRes<0>.getMax(), masterRes(0).getMax(), etc.

Well, actually, it's not an array, but a collection. And, in order to retrieve its items by index, you must use the get method:
masterRes.get(0).getMax();

masterRes.get(0).getMax();
Java/Android API document will help you.
http://androidappdocs.appspot.com/reference/java/util/ArrayList.html

Related

ArrayList for handeling many Objects?

So I want to write a programm, that can manage workouts.I have a class "Excersise" and I will create many Objects from this class. Im wondering how I should handle all these Objects. Till now I had an ArrayList with all Excersises in it but when I want to add an Excersise to a workout, I would have to do it like this: workout.add(excerises.get(index)) meaning I would have to know the index for every excerises. Also Im wondering how good the performany of ArrayLists are. Should I consider to use another data structure?
If you have constant pool of Excersises, and you only need to access them by index, then you can use array instead:
Excersise[] excersises = new Excersise[EXCERSIZE_SIZE];
//fill excersises
//use excersises
workout.add(excersises[index]);
ArrayList is designed to have fast access to element by index, and it is using array underneath, but it has range check inside it's get method. On the other hand ArrayList provide a bunch of useful methods, in case if you need any of them. The other thing is that if you would like to hold a lot of objects in it it might be wise to create it with some capacity, which you expect, knowing of how many objects you will add to this ArrayList, for example if you expect 1000 objects then you can do:
List<Excersise> excersises = new ArrayList(1000);
In that way you'll omit recreating of arrays inside ArrayList.add method when array inside ArrayList will not be able to store provided value (HashMap also has constructor HashMap(int initialCapacity)).
If you would like to access to excersise for example by it's symbolic name, let's say "push ups", then you can use HashMap:
Map<String, Excersise> excersiseByName = new HashMap<>();
//fill excersises
workout.add(excersiseByName.get("push ups"));

Can an arraylist be initialized with a method returned arraylist?

Quite new to the Java language, I was wondering whether it is correct and possible to do this:
ArrayList<PeopleDetails> people_SMS = checkbox_SMS(adapter);
Where checkbox_SMS(adapter) is a method that returns type ArrayList.
Is this legitimate code, or will the people_SMS arraylist merely contain a pointer to the the returned arraylist? Will my code work, if I want to access the data from checkbox_SMS?
Thanks for the help!!
people_SMS arraylist will merely be a pointer to the the returned arraylist. If you want to create a new list that is a copy of the returned, use
ArrayList<PeopleDetails> people_SMS = new ArrayList<>(checkbox_SMS(adapter));
It can work as long as checkbox_SMS returns an instance of ArrayList<PeopleDetails>.
I was wondering whether it is correct and possible to do this:
ArrayList<PeopleDetails> people_SMS = checkbox_SMS(adapter);
That's a completely valid line If your checkbox_sms method returning a ArrayList.. And you can use it furthur
will the people_SMS arraylist merely contain a pointer to the the returned arraylist?
Yes, it is as you are initializing with returned list.
Will my code work, if I want to access the data from checkbox_SMS?
Yes, it is.
Make sure that it is not returning a null, before using it.
Yes, this is a valid code. Inside your checkbox_SMS method you should create an instance of ArrayList<PeopleDetails> and return it.
It is also possible to return null form such a method and the code will be valid too. In such cases, you should check if people_SMS is not null before using it.

is it possible to create a regular array that hold arraylist objects?

i tried the following
ArrayList<FileFilter>[] allFilters = new ArrayList<FileFilter>()[10]
and i get
The type of the expression must be an array type but it resolved to ArrayList<FileFilter>
is it possible (i'm using java).
btw reason i am doing it is because i am trying to filter out files. some filters are or_filters, in which case i put them all in the same place in the regular array, and some are and_filters which are in another place in the regular array. is there any better idea?
Get rid of the parentheses on the right. You are actually calling the constructor for a single instance of ArrayList:
ArrayList<FileFilter>[] allFilters = new ArrayList<FileFilter>[10];
Edit: Note that this doesn't actually create any ArrayList objects; it just creates an array of 10 references, all set to null. You'll have to fill in the elements of the array.

Is it possible to create an open-ended array?

I was wondering if I could create an array without having to enter a value. I don't fully understand how they work, but I'm doing an inventory program and want my array to be set up in a way that the user can enter products and their related variables until they are done, then it needs to use a method to calculate the total cost for all the products. What would be the best way to do that?
Use an ArrayList.
This will allow you to create a dynamic array.
http://download.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html
Here is an example/overview:
http://www.anyexample.com/programming/java/java_arraylist_example.xml
Yes, you can do this. Instead of using a primitive type array, for example new int[10], use something like the Vector class, or perhaps ArrayList (checkout API docs for the differences). Using an ArrayList looks like this:
ArrayList myList = new ArrayList();
myList.add("Item 1");
myList.add("Item 2");
myList.add("Item 3");
// ... etc
In other words, it grows dynamically as you add things to it.
As Orbit pointed out, use ArrayList or Vector for your data storage requirements, they don't need specific size to be assigned while declaration.
You should get familiar with the Java Collections Framework, which includes ArrayList as others have pointed out. It's good to know what other collection objects are available as one might better fit your needs than another for certain requirements. For instance, if you want to make sure your "list" contains no duplicate elements a HashSet might be the answer.
http://download.oracle.com/javase/tutorial/collections/index.html
The other answers already told how to do it right. For completeness, in Java every array has a fixed size (length) which is determined at creation and never changes. (An array also has a component type, which never changes.)
So, you'll have to create a new (bigger) array when your old array is full, and copy the old content over. Luckily, the ArrayList class does that for you when its internal backing array is full, so you can concentrate on the actual business task at hand.

How can we get the objects in reverse order from list object?

in my List object(of generic type of pojo class objects) i am getting that list by calling service method, as per my requirement i have to place those object values in reverse order, for example
List<MyPojo> listData = serviceObj.getTableDate();//gets the total content of table
if(listData.size()>0){
int i=0;
while(i>=listData.size()){
listData.get(i).getPojoId();
listData.get(i).getPojoName();
}
}
Note: please dont tell to me use iterator or to use ListIterator, because i am showing the data in Grid(JQuery) some logic will fail while using iterator, the code what written was just sample of few lines only.
Would Collections.reverse(list) work?
Guava has Iterables.reverse(List), which returns a reversed view of the original list without modifying it.

Categories

Resources