比如: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 里对象的属性
java 循环数组,并加到对象的属性里
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
6条回答 默认 最新
- _1_1_7_ 2012-03-13 09:30关注
[code="java"]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]);
}
//说实话,你这个问题有点消耗大家的精力,^-^
}}[/code]
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 虚幻5 UE美术毛发渲染
- ¥15 CVRP 图论 物流运输优化
- ¥15 Tableau online 嵌入ppt失败
- ¥100 支付宝网页转账系统不识别账号
- ¥15 基于单片机的靶位控制系统
- ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
- ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
- ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
- ¥15 手机接入宽带网线,如何释放宽带全部速度
- ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测