问题描述
比如:String [] a=new String[] {"1","2","3","4","5","6",“7”};String [] b=new String[] {"8","9","10","11","12","13",“14”};String [] c=new String[] {"15","16","17","18","19","20",“21”};String [] d=new String[] {"22","23","24","25",“26”,"27","28",}....,Student student = new Student ();如何取到全部数组的第一个,第二个元素。。。添加到student对象的属性 里;即把1,8,15,22和2,9,16,23。。添加到student 里对象的属性 问题补充:chen_yongkai 写道
解决方案
public class Student {private String id;private String name;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public static void main(String[] args) {//数组的长度不相等String[] a = new String[] { "1", "2", "3", "4", "5", "6", "7" };String[] b = new String[] { "8", "9", "10" };int n = Math.max(a.length, b.length);Student[] stus = new Student[n];for (int i = 0; i < n; i++) {stus[i] = new Student();if (i < a.length)stus[i].setId(a[i]);if (i < b.length)stus[i].setName(b[i]);}//说实话,你这个问题有点消耗大家的精力,^-^}}
解决方案二:
public class Student {private String id;private String name;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public static void main(String[] args) { String[] a = new String[] { "1", "2", "3", "4", "5", "6", "7" }; String[] b = new String[] { "8", "9", "10", "11", "12", "13", "14" }; Student [] stus = new Student[a.length]; for (int i = 0; i < a.length; i++) { stus[i] = new Student(); stus[i].setId(a[i]); stus[i].setName(b[i]); } } }是这个意思吗?
解决方案三:
import java.util.ArrayList;public class Student {ArrayList<String> attributes = new ArrayList<String>();public void addAttribute(String attribute) {attributes.add(attribute);}public String getAttribute(int index) {return attributes.get(index);}public int sizeOfAttribute() {return attributes.size();}public static void main(String[] args) {Student s = new Student();String[] a = new String[] { "1", "2", "3", "4", "5", "6", "7" };String[] b = new String[] { "8", "9", "10", "11", "12", "13", "14" };String[] c = new String[] { "15", "16", "17", "18", "19", "20", "21" };String[] d = new String[] { "22", "23", "24", "25", "26", "27", "28" };for (int i = 0; i < 7; i++) {s.addAttribute(a[i]);s.addAttribute(b[i]);s.addAttribute(c[i]);s.addAttribute(d[i]);}int n = s.sizeOfAttribute();for (int i = 0; i < n; i++) {System.out.println(s.getAttribute(i));}}}
解决方案四:
下面是我写的一个例子,不知道能不能帮到你import java.util.LinkedHashMap;import java.util.Map;public class JavaTest {private String id;private String name;public JavaTest(String id,String name){this.id=id;this.name=name;}public String getId() {return id;}public void setId(String id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public static void main(String[] args) {String [] a=new String[] {"1","2","3","4","5","6","7"};String [] b=new String[] {"8","9","10","11","12","13","14"};String [] c=new String[] {"15","16","17","18","19","20","21"};String [] d=new String[] {"22","23","24","25","26","27","28"};Map<String,String[]> map = new LinkedHashMap<String,String[]>(); map.put("a", a); map.put("b", b); map.put("c", c); map.put("d", d); JavaTest jt=null;for(Map.Entry<String,String[]> entry:map.entrySet()){jt=new JavaTest(entry.getValue()[0],entry.getValue()[1]);}}}
解决方案五:
你的意思是把1,8,15,22和2,9,16,23做为Student的属性名称还是属性值?class Student{String id;String name;//id 和name是Student的属性}
解决方案六:
Student 对象的什么属性?Student 类的结构是怎样的?