Home > Back-end >  About the list <> assignment problem
About the list <> assignment problem

Time:10-24

I am to have such a scenario
A class aa with ID, NAME

Class bb there is such a line

List Aaa=new ArrayList<> (a);

for(int i=0; i<10; I++) {

===================
Aa temp=new (aa);
Temp. Id=I;
Temp. Name=I + "a1";
Aaa. Add (aa);
So I feel code is too wordy,
=====================

If direct use: aaa. Add (new aa () {... This how to the ID, NAME assignment})
Inside the {} to how to write,


}


I want to use now

CodePudding user response:

Aa class to write a constructor bai refs

CodePudding user response:

The
refer to the original poster cpp_1 response:
I am to have such a scene
A class aa with ID, NAME

Class bb there is such a line

List Aaa=new ArrayList<> (a);

for(int i=0; i<10; I++) {

===================
Aa temp=new (aa);
Temp. Id=I;
Temp. Name=I + "a1";
Aaa. Add (aa);
So I feel code is too wordy,
=====================

If direct use: aaa. Add (new aa () {... This how to the ID, NAME assignment})
Inside the {} to how to write,


}


Now I want to use the



First aa class need to use the following method to construct
 
Public aa (String name, int id) {
super();
this.name=name;
This. Id=id;
}


 
Aaa. The add (new aa (I + "a1", I));

CodePudding user response:

Aa the object have a constructor aa (String name, Long id)

CodePudding user response:

Code can be realized on the second floor, I can take a bubble

CodePudding user response:

Entity class there add a constructor refs

CodePudding user response:

With new aa () {} is not no, but this new object is anonymous subclasses of aa
For example
 aaa. Add (new aa () {{id=I; Name=I + "a1"; }}); 

CodePudding user response:

refer to 6th floor qybao response:
with new aa () {} is not no, but this new object is anonymous subclasses of aa
For example
 aaa. Add (new aa () {{id=I; Name=I + "a1"; }}); 

Because you I cycle of change, is the inner class to use external variables has the nature of the final, so change
 for (int I=0; i<10; I++) {
Final int j=I;
Aaa. Add (new (aa) {{id=j; Name=j + "a1"; }});
}

  • Related