用数组创建对象名称 [英] Create Object Name with Array
本文介绍了用数组创建对象名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想使用数组创建对象名称.我该怎么做?
I want to create object name using arrays. How can I do it?
例如:
String dizi ={"person1","person2","person3"};
Person dizi[0] = new Person();
推荐答案
您不能将数组中存储的对象类型从 String 更改为 Person.相反,您将需要两个单独的数组,并且假设您有一个 Person(String) 构造函数,循环遍历第一个数组以填充第二个数组:
You cannot change the type of object stored in the array from String to Person. Instead, you will need two separate arrays, and supposing you have a Person(String) constructor, loop over first array to fill second array:
String[] names = {"person1","person2","person3"};
Person[] persons = new Person[3];
for (int i = 0; i < 3; i++)
persons[i] = new Person(names[i]);
这篇关于用数组创建对象名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文