日期:2014-05-20 浏览次数:20724 次
public class teetet { public static void main(String[] args){ String s1 = "123"; String s2 = "321"; String s3 = "asd"; for(int i = 1;i<4;i++) { System.out.println("s"+i); } } }
package text4; import java.util.ArrayList; import java.util.List; public class Demo1 { public static void main(String args[]){ List<String> list = new ArrayList<String>(); list.add("123"); list.add("234"); list.add("dfg"); for(String s:list){ System.out.println(s); } } }
------解决方案--------------------
Java不是动态语言,所以只能。。。
public static void main(String[] args) { String s1 = "123"; String s2 = "321"; String s3 = "asd"; Map<String, Object> valMap = new HashMap<String, Object>(); valMap.put("s1", s1); valMap.put("s2", s2); valMap.put("s3", s3); for (int i = 1; i < 4; i++) { System.out.println(valMap.get("s" + i)); } }
------解决方案--------------------
import java.util.ArrayList;
import java.util.List;
public class teetet {
public static void main(String[] args){
String s1 = "123";
String s2 = "321";
String s3 = "asd";
List<String> result = new ArrayList<String>();
result.add(s1);
result.add(s2);
result.add(s3);
for (int i = 0; i < 3; i++) {
System.out.println(result.get(i));
}
}
}
------解决方案--------------------
public class teetet {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] s={"123","321","asd"};
for(int i = 0;i<s.length;i++)
{
System.out.println(s[i]);
}
}
}
可以用用String类型的数组赋值啊,或者用集合