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.
Related
I want save an object multiple times, the code below can not work:
for(int i = 0; i < 5; i++) {
repository.save(object);
}
Then I change the source to:
List<SomeObject> objectList = new ArrayList<>();
for(int i = 0; i < 5; i++) {
objectList.add(object);
}
repository.save(object);
But it also can not work, It only save the object one time to database.
Maybe because it point to one instance, but I want to know if there is an easy way to save an object multiple times?
Thank you!
It saves only 1 time the object because you are using the same instance every times.
So the first time you use object, so now it has an ID, then you use object again, but as we said it has an id so instead to save it, yours program update it inside you db.
For example if you do this:
for(int i = 0; i < 5; i++) {
repository.save(new Object());
}
Or if you have a list of Objects, like
List<SomeObject> objectList = new ArrayList<>();
for(int i = 0; i < 5; i++) {
objectList.add(object);
}
you could do this:
repository.saveAll(objectList);
Now you find 5 rows inside your table
I am not familiar with Spring data and might misunderstand the problem but generally there is no point to save the same object many times.
You need to explicitly create a clone/copy (or so) of an instance and save all these instances separately if you need multiple instances in database.
For example add creating new instance in a loop that saves the object.
Using the constructor, you can save same object multiple time:
Repository.save(new object(String name,String age));
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.
Is this usage of elements of an ArrayList:
for(int i=0; i<array_list.size(); i++){
Object obj = array_list.get(i);
//do **lots** of stuff with **obj**
}
faster than this one:
for(int i=0; i<array_list.size(); i++){
//do **lots** of stuff with **array_list.get(i)**;
}
It depends on how many times array_list.get(i) is called in the second code. If it is called only once, there is no difference between both methods.
If it's invoked multiple times, saving the value in a variable may be more efficient (it depends on the compiler and the JIT optimizations).
Sample scenario where the first method may be more efficient, compiled using Oracle JDK's javac compiler, assuming the list contains String objects:
for(int i=0; i<array_list.size(); i++){
String obj = array_list.get(i);
System.out.println(obj);
if(!obj.isEmpty()) {
String o = obj.substring(1);
System.out.println(o + obj);
}
}
In this case, obj is saved as a local variable and loaded whenever it is used.
for(int i=0; i<array_list.size(); i++){
System.out.println(array_list.get(i));
if(!array_list.get(i).isEmpty()) {
String o = array_list.get(i).substring(1);
System.out.println(o + array_list.get(i));
}
}
In this case, multiple invokation for List.get are observed in the bytecode.
The performance difference between getting once and a local variable is almost always neglible. But... if you insist on doing it the hardcore way, this is the fast way to go:
ArrayList<Object> array_list = ...
// cache list.size() in variable!
for (int i=0, e=array_list.size(); i < e; ++i) {
// get object only once into local variable
Object object = array_list.get(i);
// do things with object
}
It caches the lists size into a local variable e, to avoid invoking array_list.size() at each loop iteration, as well as each element in the loop to avoid get(index) calls. Be aware that whatever you actually do with the objects in the loop will most likely be by orders of magnitude more expensive than the loop itself.
Therefore, prefer code readability and simply use the advanced for loop syntax:
ArrayList<Object> array_list = ...
for (Object object : array_list) {
// do things with object
}
No hassles, short and clear. Thats worth far more than a few saved clock cycles in most cases.
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.
I have a problem where I retrieve and element from a list (list1), and modify one of the parameters in the element and then add it to another list (list2). When I do this to the final item in list1, it will sometimes modify the parameters of the elements in list2.
This function is called once per generation, but it is not until about the 8th generation when I start to see this happen. It has affect anywhere from 2 to 16 elements in list2.
Any ideas where my screw up might be? Here's a block of code I wrote to illustrate the problem. The problem occurs in the section where I check count==0.
public void sampleCode (List list) {
List differentList = new ArrayList();
Individual element;
Individual differentelement;
int i = 0;
int count = 0;
for(i = 0; i < list.size(); i++) {
element = (Individual) list.get(i) ;
// does some checking to see if this meets criteria
// this is sorta pseudo code
if(probability == true) {
element.setDoMutation(true);
count++;
}
//always add this element to differentList
//even if no changes are made to the element
differentList.add(i,element);
}
//need to make sure one elements has mutation=true;
if(count == 0) {
differentelement = (Individual) list.get((list.size()-1));
//setting this element field changes the contents of
//different list.
differentelement.setDoMutation(true);
differentList.set((list.size()-1), differentelement);
}
}
In Java, a variable doesn't hold an object. It holds an object reference (i.e. a pointer to an object). Getting an object fom a list and putting it in another list doesn't make a copy of the object. Both lists simply have a pointer to the same object. So, of course, if you modify the contents of the object, both lists will have the object modified.
Side note: You should use parameterized types (i.e. List<Individual> rather than List), and avoid declaring variables at the beginning of your methods as you would do in C. Only declare a variable when you need it. This will make the code much clearer, and reduce the scope of your variables.
Are you sure that the block
//need to make sure one elements has mutation=true;
if(count == 0) {
differentelement = (Individual) list.get((list.size()-1));
//setting this element field changes the contents of
//different list.
differentelement.setDoMutation(true);
differentList.set((list.size()-1), differentelement);
}
has to be inside the loop
for(i = 0; i < list.size(); i++) {
...
}
I suspect it has to be moved after the loop in order to work correctly.