java怎么创建一个对象来存放数组的值。
我有三个数组string[]Type;string[]id;string[]uesername,他们长度都相同,且都为8。他们中的{type[0],id[0],usern...
我有三个数组string[]Type;string[] id;string[] uesername,他们长度都相同,且都为8。他们中的{type[0],id[0],username[0]}{type[1],id[1],username[1]}以此类推直到7都是一组。请问怎么把他们筛选出来放入一个对象。要每个都分开来!求代码!
展开
4个回答
展开全部
public class Test{
public static void main(String[] args) {
User[] users = new User[8];
Test t = new Test();
users[0] = t.new User("1", "type", "蠢衡username");
users[1] = t.new User("2", "type", "username");
users[2] = t.new User("3", "type", "username");
users[3] = t.new User("4"运搜, "type", "username");
users[4] = t.new User("5", "type", "username");
users[5] = t.new User("6"带悄做, "type", "username");
users[6] = t.new User("7", "type", "username");
users[7] = t.new User("8", "type", "username");
}
class User{
public User(String id, String type, String username){
this.id = id;
this.type = type;
this.username = username;
}
public String type;
public String id;
public String username;
}
}
public static void main(String[] args) {
User[] users = new User[8];
Test t = new Test();
users[0] = t.new User("1", "type", "蠢衡username");
users[1] = t.new User("2", "type", "username");
users[2] = t.new User("3", "type", "username");
users[3] = t.new User("4"运搜, "type", "username");
users[4] = t.new User("5", "type", "username");
users[5] = t.new User("6"带悄做, "type", "username");
users[6] = t.new User("7", "type", "username");
users[7] = t.new User("8", "type", "username");
}
class User{
public User(String id, String type, String username){
this.id = id;
this.type = type;
this.username = username;
}
public String type;
public String id;
public String username;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
如果是定碰租散值非常好弄;
Object [][]obj = new Object [8][3];//如果确认是型衫String ,可以直接定笑氏义成String 类型
for(int i = 0; i<8;i++){
obj[i][0] = Type[i];
obj[i][1] = id[i];
obj[i][2] = username[i];
}
//obj里面应该存储的就是你想要的东西了。
Object [][]obj = new Object [8][3];//如果确认是型衫String ,可以直接定笑氏义成String 类型
for(int i = 0; i<8;i++){
obj[i][0] = Type[i];
obj[i][1] = id[i];
obj[i][2] = username[i];
}
//obj里面应该存储的就是你想要的东西了。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
把数字中的每项作为对象中的一个字段
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/颤枯空/新建一个类User用来存放这,三个数据
//结果放到一个User数组中,你看败滚这可以吗?
/茄瞎/还是说要放到一个list中?
public class ObjectTest {
public static void main(String[] args) {
String[] id = new String[8];
String[] type = new String[8];
String[] username = new String[8];
User[] users = getUsers(type, id, username);
}
private static User[] getUsers(String[] type, String[] id, String[] username) {
User[] users = new User[type.length];
for (int i = 0; i < type.length; i++) {
users[i] = new User(id[i], type[i], username[i]);
}
return users;
}
}
class User {
public User(String id, String tpye, String username) {
this.tpye = tpye;
this.id = id;
this.username = username;
}
private String tpye;
private String id;
private String username;
public String getTpye() {
return this.tpye;
}
public String getId() {
return this.id;
}
public String getUsername() {
return this.username;
}
public void setTpye(String tpye) {
this.tpye = tpye;
}
public void setId(String id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
}
//结果放到一个User数组中,你看败滚这可以吗?
/茄瞎/还是说要放到一个list中?
public class ObjectTest {
public static void main(String[] args) {
String[] id = new String[8];
String[] type = new String[8];
String[] username = new String[8];
User[] users = getUsers(type, id, username);
}
private static User[] getUsers(String[] type, String[] id, String[] username) {
User[] users = new User[type.length];
for (int i = 0; i < type.length; i++) {
users[i] = new User(id[i], type[i], username[i]);
}
return users;
}
}
class User {
public User(String id, String tpye, String username) {
this.tpye = tpye;
this.id = id;
this.username = username;
}
private String tpye;
private String id;
private String username;
public String getTpye() {
return this.tpye;
}
public String getId() {
return this.id;
}
public String getUsername() {
return this.username;
}
public void setTpye(String tpye) {
this.tpye = tpye;
}
public void setId(String id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
}
更多追问追答
追问
放进list吧。
追答
import java.util.ArrayList;
import java.util.List;
public class ObjectTest {
public static void main(String[] args) {
String[] id = new String[8];
String[] type = new String[8];
String[] username = new String[8];
List userlist = getUsers(type, id, username);
}
private static List getUsers(String[] type, String[] id, String[] username) {
List list = new ArrayList();
for (int i = 0; i < type.length; i++) {
list.add(new User(type[i], id[i], username[i]));
}
return list;
}
}
class User {
public User(String tpye, String id, String username) {
this.tpye = tpye;
this.id = id;
this.username = username;
}
private String tpye;
private String id;
private String username;
public String getTpye() {
return this.tpye;
}
public String getId() {
return this.id;
}
public String getUsername() {
return this.username;
}
public void setTpye(String tpye) {
this.tpye = tpye;
}
public void setId(String id) {
this.id = id;
}
public void setUsername(String username) {
this.username = username;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询