如何将Object的一个元素转换为实例数组?

时间:2014-05-28 21:57:49

标签: java android arrays class instance

我有这个课程People,其中包含字段名称,电话,电子邮件和地址。这个类有很多例子。如何获取name元素的所有实例的数组。例如, String[] array = "John, Tim, Harry"。所以,我想做的就是从每个实例中取出一个元素并创建它们的数组。我一直在研究这个问题。非常感谢帮助。顺便说一下,所有字段都是字符串。

3 个答案:

答案 0 :(得分:2)

我不确定这是不是你的意思,但我会展示两种方式:

如果您将Person的实例声明为单个变量,它将如下所示:

Person p1 = new Person("name1");
Person p2 = new Person("name2");
Person p3 = new Person("name3");

(我忽略了其他人的字段,以使这更容易。)

现在你可以将这些人放在Person [](人物阵列)

Person[] persons = new Person []{p1, p2, p3};

现在我们将循环使用这些人(我说你在Person类中有.getName()方法)

String[] names = new String[persons.length]; // Create names Array
for (int i = 0; i < persons.length; i++)     //Loop the persons Array
{
    names[i] = persons[i].getName();         //Take the name from persons in
                                             //position [i] and put it in position
                                             //[i] of the names Array.
}

另一种方法是直接放置名称:

String[] names = new String[]{p1.getName(), p2.getName(), p3.getName()};

答案 1 :(得分:2)

1 - 创建人物模型

public class People {
private String name;
private String phone;
private String address;
public People(String name, String phone, String address) {
    this.name = name;
    this.phone = phone;
    this.address = address;
}
/**
 * @return the name
 */
public String getName() {
    return name;
}
/**
 * @param name the name to set
 */
public void setName(String name) {
    this.name = name;
}
/**
 * @return the phone
 */
public String getPhone() {
    return phone;
}
/**
 * @param phone the phone to set
 */
public void setPhone(String phone) {
    this.phone = phone;
}
/**
 * @return the address
 */
public String getAddress() {
    return address;
}
/**
 * @param address the address to set
 */
public void setAddress(String address) {
    this.address = address;
}

}

2 - 创建自定义适配器

public class PeopleListAdapter extends BaseAdapter {

private Context context;
private ArrayList<People> peopleItems;

public PeopleListAdapter(Context context,
        ArrayList<People> peopleItems) {
    this.context = context;
    this.peopleItems = peopleItems;
}

@Override
public int getCount() {
    return peopleItems.size();
}

@Override
public Object getItem(int position) {
    return peopleItems.get(position);
}

@Override
public long getItemId(int position) {
    return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) context
                .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.people_list_item, null);
    }
    // TODO
    /**
     * Return some list row..
     */
    TextView nameTxt = (TextView) convertView.findViewById(R.id.name);
    TextView phoneTxt = (TextView) convertView.findViewById(R.id.phone);
    TextView addressTxt = (TextView) convertView.findViewById(R.id.address);

    nameTxt.setText(peopleItems.get(position).getName());
    phoneTxt.setText(peopleItems.get(position).getPhone());
    phoneTxt.setText(peopleItems.get(position).getAddress());

    return convertView;
}

}

<强>更新

private ArrayList<People> peopleList = new ArrayList<People>();
peopleList.add(new People("name1", "phone1","address1");
peopleList.add(new People("name2", "phone2","address2");

// adaptor
PeopleListAdapter adaptor = new PeopleListAdapter(getApplicationContext(),peopleList);
//some list view
listView.setAdapter(adapter);

答案 2 :(得分:1)

您可以循环遍历People类的每个实例,并将每个名称存储在ArrayList中。然后使用toArray()将ArrayList转换为String。有点像这样:

ArrayList<String> names = new ArrayList<String>();
for (People p1 : myList) names.add(p1.getName());
String allnames[] = names.toArray(new String[names.size()]);