i wanted to know how i can iterate through a collection of classes and be able to use the classes functions on every pass.
Im new to java and im much more familier with c++.
This is the collection i wish to iterate..
private ArrayList<Album> albumCollection;
and activate the following function
get title
This is my current code...
//Lists all stored titles
private void ListAllTitles(){
int size = albumCollection.size();
for(int i=0; i < size; i++){
System.out.println(albumCollection(i).getTitle());
}
}
In order to access an element from a List, you need to use the get method:
System.out.println(albumCollection.get(i).getTitle());
Also note, you could use the for each loop to achieve this:
for (Album album : albumCollection) {
System.out.println(album.getTitle());
}
This for each construct is simpler:
for (Album album : albumCollection)
{
System.out.println(album.getTitle());
}
Related
private void displayGroupsInRanking() {
for (int i = 0; i < 4; i++)
{
RankingANames.setItems(FXCollections.observableArrayList(groupModel.getListA().get(i).getName()));
System.out.println(RankingANames);
}
RankingBNames.setItems(FXCollections.observableArrayList(groupModel.getListB()));
RankingCNames.setItems(FXCollections.observableArrayList(groupModel.getListC()));
RankingDNames.setItems(FXCollections.observableArrayList(groupModel.getListD()));
}
I´m trying to, to get a specific attribute from an arraylist into a new arraylist. This works fine, but the listview only shows one object?
[The output1
the Code
I'll not rewrite code from your screen to show you the right way to do this but I can tell you what is wrong here.
On every iteration you are creating new collection with exactly one item and then you are using it as items list for table.
That's clearly wrong.
To solve it, you have to first prepare full list of items and then pass it to setItems method.
I have a class Section with several methods including methods get_key() and get_angle(). Items of type Section are added to a hashtable implemented in class Hashtable.
According to my task I should delete such elements from the hashtable which have bigger value of function get_angle() than given_value.
class Hashtable{
private Section[] hash_array; //array of cells of the hashtable
public int size;
public void remove_given(double given_value)
{
for(int i = 0; i < size; i++)
{
if (hash_array[i] != null)
{
double value = hash_array[i].get_angle(); //value of needed function to compare
if (value > given_value)
{
int key_ = hash_array[i].get_key(); //get key for the item in order to delete it
Delete(key_); //delete item
}
}
}
}
}
But the method doesn`t delete any elements. I checked the method Delete() separately and it works just fine as well as other methods called on this method . I really need to figure it out. So I will be grateful for your help.
Debug your code, does it enter the for-loop. How do you initialize the value of size variable? If you forget to initialize it by default it will be zero. It is better to get the size from the hash_array.length.
For one thing you're using the uninitialized global var, size, the size used in the for loop needs to be the size of the Hash collection. Also how is the Hash initialized? Does it contain what you think? I'd follow the aforementioned suggestion to step through the code with a debugger, perhaps the keys aren't what you think they are...
I have the following code..
WorkPackage spack=(WorkPackage)primaryBusinessObject;
WTSet res;
WTPart spart=null;
String state=null;
res=wt.facade.persistedcollection.PersistedCollectionHelper.service.getAllMembers(spack);
System.out.println("the values are "+res);
java.util.Iterator iter=res.persistableIterator();
while(iter.hasNext())
{
spart=(wt.part.WTPart) iter.next();
wt.lifecycle.LifeCycleState st=spart.getState();
String state=st.toString(); //Lifecycle state of part object
}
if(state.contains("APPROVED"))
result="Proceed";
In the above code I'm passing a windchill package and it may have muliple number of WTPart objects.Each part may have different life cycle states.What I want is if every part state is "APPROVED" means it should proceed in my workflow.
For eg.
LifeCycle states of
Part1=IN WORK
Part2=IN REVIEW
Part3=APPROVED
Part4=APPROVED
Part5=CANCELED
I want to compare all the objects from my package is APPROVED I can store these in vector or a arraylist and I don't know how to compare all the objects from that.My above code will pass if any one of objects state is APPROVED.I know this question not related to windchill.Somebody help me out of it
If you store all of the states in an ArrayList<String> states then you can test if they are all APPROVED using something like:
boolean allApproved = true;
for(int i = 0; i < states.size(); i++) {
if(!states.get(i).equals("APPROVED") {
allApproved = false;
}
}
At the end of the for loop, if allApproved is still true, you're good to go.
I have an 8x8 board with 64 four fields, each being a LinearLayout.
Each LinearLayout has its ID like (field1, field2... field64).
Now I want to modify the background of each field later in my program but I don't know how to retrieve the layout I want to modify. I wanted to put all of them in the Array but it's not working.
private LinearLayout[] fields = new LinearLayout[65];
this.fields[field_id].setBackgroundColor(colour);
Another approach:
for (int i=1; i<65; i++) {
findViewById(R.id.field+i).setBackgroundColor(Color.WHITE);
}
It does not work as well (obviously). How should I approach this?
You can add them to a list, like this:
List fieldList = new ArrayList(64);
fieldList.add(R.id.field1);
fieldList.add(R.id.field2);
fieldList.add(R.id.field3);
// etc.
fieldList.add(R.id.field64);
Or use any other method to create such a list/array. It is a bit tedious, adding all fields like this, but it provides a usable list that can be used in loops.
It is also possible (I think), to renumber the ids of the fields (in R.java). If you make them subsequent, you can use the method you tried in the example code of your question. I do not not for sure whether these IDs stay the same when R.java is re-generated... If not, this method is not usable!
Make an Array with your ids.
int[] ids = {R.id.field1,R.id.field2...}
for (int i=0; i<ids.lenght; i++) {
findViewById(ids[i]).setBackgroundColor(Color.WHITE);
}
OR
ViewGroup viewgroup = findViewById(R.id.parentview);
for(int i = 0; i < viewgroup.getChildCount();i++)
{
View child = viewgroup.getChildAt(i);
}
You can access them this way, a tiny bit slower but doubt you will have a problem with it:
for (int i=1; i<65; i++) {
int id = getResources().getIdentifier("field" + i, "id", getPackageName())
findViewById(id).setBackgroundColor(...);
}
Sounds like a lot of work adding them in xml(I assume?) like that though. Preferably you would add them by code and then just save them in an array.
What I want is to create multiple view in for loop
example
for(int i =1; i<5; i++){
GridView view = new Gridview(this);
}
But It creates 5 gridview with the same name.. so in future i can't set different option to a specific gridview. How do I get, that gridivew created in a loop get view + i name
Use a List
List<GridView> views = new ArrayList<GridView>();
for(int i = 1; i < 5; i++) {
views.add(new GridView(this));
}
Then you can get your views with
views.get(i);
Also, in your example, when a step of the for loop ends, the reference to that object is lost as the scope in which they were created is left. With no reference to the objects, the Garbage Collector comes in and frees that memory, deleting the objects.
So, you won't be able to access not even the last object created. If you modify the code like this, at the end of this code you will have only the last object instantiated:
GridView view;
for(int i =1; i<5; i++){
view = new Gridview(this);
}
Now the object exists in the scope you are in at the end of the code snippet. But only one object really exists.
So, the solution is to store the objects in some additional structure: an array if you know precisely how many objects you want, or some self dynamically allocated collection structure. And you have examples of both in the other answers.
Added: What you are actually asking for (to dynamically build the object's reference name) is called metaprogramming. I don't know if it is possible in Java, but here is an example of this done in PHP:
class Object {
function hello(){
echo "Hello \n";
}
}
for($i =1; $i<5; $i++){
$name = "view".$i;
$$name = new Object();
}
$view1->hello();
$view2->hello();
$view4->hello();
Here is runnable code: http://codepad.org/bFqJggG0
Use a List of GridViews.
List<GridView> grids = new ArrayList<GridView>();
for(int i =1; i<5; i++){
grids.add(new Gridview(this));
}
I think this code will create 5 GridViews, 4 of which will become immediately available for Garbage Collection as your code no longer has a reference to them.
If you create them in a loop, then I think I'd be looking to store them in a data structure such as a List or Map and then accessing them via an index or key.
i think you can do something like this :
for(int i =1; i<5; i++){
GridView view = new Gridview(this);
view.setId(i);
}
and then , you can difference between all views
GridView[] view=new GridView[5];
for(int i=1;i<5;i++){
view[i]=new GridView(this);}
Now I think you can set specified options.