String[] t5 = null;
t5[0] = "0001 01";
t5[1] = "0001 02";
t5[2] = "0001 03";
t5[3] = "0002 01";
t5[4] = "0002 04";
t5[5] = "0044 05";
t5[6] = "0044 12";
....输出:
t5[0]="0001 01 02 03"
t5[1]="0002 01 04"
t5[2]="0044 05 12"
数组中的值不确定。
t5[0] = "0001 01";
t5[1] = "0001 02";
t5[2] = "0001 03";
t5[3] = "0002 01";
t5[4] = "0002 04";
t5[5] = "0044 05";
t5[6] = "0044 12";
....输出:
t5[0]="0001 01 02 03"
t5[1]="0002 01 04"
t5[2]="0044 05 12"
数组中的值不确定。
t5[0] = "0001 01";
t5[1] = "0001 02";
t5[2] = "0001 03";
t5[3] = "0002 01";
t5[4] = "0002 04";
t5[5] = "0044 05";
t5[6] = "0044 12";你这样写输出时不会抛出异常吗??!
t5[0] = "0001 01";
t5[1] = "0001 02";
t5[2] = "0001 03";
t5[3] = "0002 01";
t5[4] = "0002 04";
t5[5] = "0044 05";
t5[6] = "0044 12";
-------------------------------------------
t5为null
你怎么还能访问它的成员????
public static void main(String args[]){
HashMap <String,Value>map = new HashMap<String,Value>();
String str[] = new String[7];
str[0] = "0001 01";
str[1] = "0001 02";
str[2] = "0001 03";
str[3] = "0002 01";
str[4] = "0002 04";
str[5] = "0044 05";
str[6] = "0044 12";
for(int i=0; i<str.length; i++){
String key = str[i].substring(0,str[i].lastIndexOf(' '));
String val = str[i].substring(str[i].lastIndexOf(' ')+1); if(map.containsKey(key)){
map.get(key).value +=" "+val;
}
else
map.put(key,new Value(val));
}
ArrayList <String>result = new ArrayList<String>();
Iterator it = map.entrySet().iterator();
while(it.hasNext()){
result.add(it.next().toString().replace('=',' '));
}
for(int i=0; i<result.size(); i++){
System.out.println(result.get(i));
}
}
}class Value{
String value;
public Value(String str){
value = str;
}
public String toString(){
return value;
}
}
import java.util.*;public class Composition {
HashMap map = new HashMap();
private void putItToMap(String key, String val){
if(map.containsKey(key)){
Value value = (Value) map.get(key);
value.value +=" "+val;
map.put(key, value);
}
else
map.put(key,new Value(val));
}
public void processStrings(String[] strs){
for(int i = 0; i < strs.length; i++){
String key = strs[i].substring(0, strs[i].lastIndexOf(' '));
String val = strs[i].substring(strs[i].lastIndexOf(' ') + 1);
putItToMap(key, val);
}
}
public ArrayList getResult(){
ArrayList result = new ArrayList();
Iterator it = map.entrySet().iterator();
while(it.hasNext()){
result.add(it.next().toString().replace('=',' '));
}
return result;
}
public void displayResult(ArrayList al){
for(int i = 0; i < al.size(); i++){
System.out.println(al.get(i));
}
}
public static void main(String args[]){
String strs[] = new String[7];
strs[0] = "0001 01";
strs[1] = "0001 02";
strs[2] = "0001 03";
strs[3] = "0002 01";
strs[4] = "0002 04";
strs[5] = "0044 05";
strs[6] = "0044 12";
Composition c = new Composition();
c.processStrings(strs);
c.displayResult(c.getResult());
}
}class Value{
String value;
public Value(String str){
value = str;
}
public String toString(){
return value;
}
}
而且你在给数组赋值之前没有创建对象,会抛出NullPointerException的
分割可以这样写的
String key = strs[i].split(" ")[0];
String args[];
args[]=new String[5];
args[0]=new String("hello0");
args[1]=new String("hello1");
args[2]=new String("hello2");
args[3]=new String("hello3");
args[4]=new String("hello4");
args[]里面保存的实际是各个STRING对象的引用,并没有字符串在内,真正的字符串是通过“args[0]”这些引用来指向的,好比数组是一个抽屉,里面的各个对象实际只是一把把钥匙,这一把把的钥匙对应的抽屉里的物件(对象,实际内存空间)才是我们要存储和提取的。