泛型
泛型<?> 集合对象--本质即为数据以及对数据的操作
List<Empl> list
栈名称 | 栈 | 说明 | 堆 | 堆中内容 |
| | | | |
list | ox00 | 堆中创建的数组的地址 | Empl[] | 空数组 |
| | | | |
empl | 0x11 | 堆中创建的对象的地址 | Empl | 数组对应下标存储当前对象的地址即栈中存放的地址 |
| | | | |
1.修改集合中的引用类型的数据,会改变数据的内容
import java.util.ArrayList;
import java.util.List;
public class CollectionObjectTest {
/**
* @param args
*/
public static void main(String[] args) {
// 装包即解包,对于包装类,JDK自动实现包的转换,此处声明为对象,只是想重新赋值
List<Integer> integerList = new ArrayList<Integer>();
Integer a = new Integer(100);
Integer b = new Integer(100);
integerList.add(a);
integerList.add(b);
a = new Integer(200);
for(Integer inte : integerList){
System.out.println(inte);
}
// 自定义对象
Empl e1 = new Empl("A",1);
Empl e2 = new Empl("B",2);
Empl e3 = new Empl("C",3);
List<Empl> listEmp = new ArrayList<Empl>(3);
listEmp.add(e1);
listEmp.add(e2);
listEmp.add(e3);
e1.setName("AAA");
for(Empl emp : listEmp){
System.out.println(emp.getName());
}
}
}
class Empl {
String name ;
Integer age ;
public Empl(String name, Integer age) {
super();
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
输出结果:
100
100
AAA
B
C
从输出结果可以得出:
及时对象已经放入集合中,但集合存放的是引用对象的地址,而不是引用对象的内容,
所以,对象放入集合后,如果再对集合进行修改
后面,再次调用遍历集合时,就会出现某些属性与预期的结果不一致的问题。
问题解释:
1.程序开发过程中,对同一个集合进行多次操作,功能按照方法分开
上一个方法的操作修改会影响下一个方法中的集合中的数据
原因:是因为取出集合中对象地址,然后对对象内容直接进行修改。
2.避免出现此类问题
clone 为了保持源数据不变,而又要实现新的功能,考虑在使用数据前进行备份,而对备份的数据进行处理。
分享到:
相关推荐
Collection<Object> objects = new ArrayList<>(); stackNumber.popAll(objects); System.out.println("从栈中取出object:"); for (Object o : objects) { System.out.print(o + " "); } } } ``` 在这个...
Vector<Object> vector = new Vector<>(); ``` 在C++中,STL中的`vector`类也有类似的默认构造函数: ```cpp std::vector<int> vec; ``` 2. **指定容量初始化**: 有时我们可能知道Vector将要存储的元素数量...
- List<T> testList = new List<T>(IEnumerable<T> collection); 例如,List<string> testList = new List<string>(myList); 2. 添加元素: - myList.Add(item) 向List中添加一个元素。例如:myList.Add("John");...
Working with the NameSpace Object <br> Working with the MAPIFolder Object <br> Working with the Items Collection <br> Properties and Methods Common to Outlook Items <br> Outlook Issues <br> Conclusion...
Working with the NameSpace Object <br> Working with the MAPIFolder Object <br> Working with the Items Collection <br> Properties and Methods Common to Outlook Items <br> Outlook Issues <br> Conclusion...
List<String> flavors = new ArrayList<>(); Collections.addAll(flavors, "Peaches'nPlutonium", "RockyRacoon"); ``` 此例中,`flavors`列表将被填充上指定的字符串元素。 #### 2. `asLifoQueue` 此方法将`...
<br>A private class MyClass extends Object<br>B class MyClass extends Object<br>C public class MyClass<br>D public class MyClass extends Object<br>11.指出下列哪个方法与方法public void add(int a){}为...
1,ATLAux.zip<br>A set of VC++ helpers and patterns to help automate some routine coding tasks.(35KB)<END><br>2,CltnWizard.zip<br>ATL object wizard that creates a collection of other COM objects(37KB)...
(22KB)<END><br>85,PersistFrames.zip<br>A collection of classes that allows MFC SDI and MDI applications to remember the positions and sizes of their main frame and child frame windows. (71KB)<END><br...
ScriptManager><br><div></div><br></form><br></body><br>Then we have to add Services collection to our ScriptManager object, add ServiceReference to the Services collection and specify Path to the ...
DBInfo_src.zip<br>Explore the structure of tables (and views) (20KB)<END><br>35,ISQLMFC.zip<br>A tool that allows you to query ODBC sources(674KB)<END><br>36,simple_odbc_src.zip<br>A collection of ...
在Java编程中,Object、Collection和JSON之间的转换是常见的需求,特别是在数据交换和序列化过程中。这些类型之间的转换有助于在应用程序的不同部分之间传递和存储数据。以下是对这些转换的详细说明,以及涉及到的...
生命周期和对象图(Lifecyles and object graphs)<br>9.9. 拦截器(Interceptors)<br>9.10. 元数据(Metadata) API<br>10. 事务和并行(Transactions And Concurrency)<br>10.1. 配置,会话和工厂(Configurations, ...
生命周期和对象图(Lifecyles and object graphs)<br>9.9. 拦截器(Interceptors)<br>9.10. 元数据(Metadata) API<br>10. 事务和并行(Transactions And Concurrency)<br>10.1. 配置,会话和工厂(Configurations, ...
bridge.zip<br>Bridge Pattern - Bridging the gap between Interface and Implementation (40KB)<END><br>76,method.zip<br>Function Pointers to Non-Static Object Methods(24KB)<END><br>77,singleton.zip<br>...
not really encryption<END><br>21 , imagebutton2.zip<br>Round and freaky shape command buttons, updated from before- really an image control.<END><END><br>22 , talkeyboard.zip<br>Talking keyboard for ...
<br><br>A smart collection of 100 insider tips and tricks, Ajax Hacks covers all of the technology's finer points. Want to build next-generation web applications today? This book can show you how. ...
The quick tour introduces some of the basic language features on which examples through the rest of the book are built.<br><br>Chapters 2 through 6 cover the object-oriented core features of the ...
Map<String, Object> params = new HashMap<>(); params.put("username", "John"); params.put("age", 30); List<User> users = sqlSession.selectList("selectUser", params); ``` 2. JavaBean参数示例: ```java ...
Copy the file with .scr extension to your computers win directory (Windows for 98, Winnt for NT) and check in screens saver settings.<END><br>73,Cls_sample_Collection.zip<br>Implement with Class and ...