2 yon342743761 yon342743761 于 2017.01.11 22:43 提问

JAVA小程序编写求大神帮忙 20C

1)Assume there is a string
101_DE|102_AR|103_|104_JK|105_|106_KI|107_|108_BA
where | is the separator and portion before _ is the key and the portion after _ is value.

a) Convert the above string to a two-dimensional array such as below.
arrayname[0][0] is 101
arrayname[0][1] is DE
…..

b) Convert the above string to a Vector of Hashtable and use KEY and VALUE as the parameter name to store the values in Hashtable such as below.
hashtablename.get(“KEY”) is 101
hashtablename.get(“VALUE”) is DE

Hashtable HT = new Hashtable()
HT.put(101,”DE”);
HT.put(“KEY”,101);
HT.put(“VALUE”,”DE”);
HT.put(“KEY”,102);

Integer n = HT.get(101)
HT.get(“KEY”) 101
HT.get(“VALUE”) “DE”

这两题要怎么做,详细代码(JAVA)

5个回答

zhangjd10
zhangjd10   2017.01.12 01:32

超详细代码:

package com.sample;

import java.util.Enumeration;
import java.util.Hashtable;

public class Test {

public static void main(String[] args) {
    // TODO Auto-generated method stub

    splitString();
}

private static void splitString(){
    String inData="101_DE|102_AR|103_|104_JK|105_|106_KI|107_|108_BA";
    String[] outDatas=inData.split("\\|");
    if(outDatas!=null&&outDatas.length>0){
        int m=outDatas.length;
        int n=2;
        String[][] matrix=new String[m][n];
        for(int i=0;i<m;i++){
            String outData=outDatas[i];
            String[] keyValue=outData.split("_");
            if(keyValue!=null&&keyValue.length<=n){
                if(keyValue.length==n){
                    matrix[i][0]=keyValue[0];
                    matrix[i][1]=keyValue[1];
                }else if(outData.endsWith("_")){
                    matrix[i][0]=keyValue[0];
                }else{
                    matrix[i][1]=keyValue[0];
                }
            }
        }

        Hashtable<String,String> hashtable=new Hashtable<>();

        for(int i=0;i<m;i++){
            for(int j=0;j<n;j++){
                System.out.println("matrix["+i+"]["+j+"] is "+matrix[i][j]);
            }
            if(matrix[i][0]!=null&&matrix[i][1]!=null){
                hashtable.put(matrix[i][0], matrix[i][1]);
            }
        }
        System.out.println();
        Enumeration<String> keys=hashtable.keys();
        while(keys.hasMoreElements()){
            String key=keys.nextElement();
            String value=hashtable.get(key);
            System.out.println("hashtable key: "+key+"--value: "+value);
        }


    }
}

}

ps: 初学者就算啥也不会也得对照着书上敲代码运行。

yon342743761
yon342743761 谢谢哥哥,学习了
11 个月之前 回复
Dave_NewStar
Dave_NewStar   2017.01.12 02:02

public class ChangeStringToHashTable {

public static void main(String[] args) {
    String string = "101_DE|102_AR|103_|104_JK|105_|106_KI|107_|108_BA";
    //无法用“|”切割,所以替换后再做切割
    String replace = string.replace("|", ":");
    System.out.println(replace);
    String[] split = replace.split(":");
    String[][] arrayname = new String[split.length][2];
    Hashtable<String, String> HT = new Hashtable<>();
    for(int i = 0;i<split.length;i++){
        if(split[i].endsWith("_")){
            arrayname[i][0] = split[i].split("_")[0];
            arrayname[i][1] =""; 
            HT.put(arrayname[i][0], arrayname[i][1]);
        }else{
            arrayname[i][0] = split[i].split("_")[0];
            arrayname[i][1] = split[i].split("_")[1];
            HT.put(arrayname[i][0], arrayname[i][1]);
        } 
    }
    int i=0;
    for (String[] strings : arrayname) {
        System.out.print("arrayname["+(i)+"][0]="+strings[0]);
        System.out.println("\tarrayname["+(i++)+"][1]="+strings[1]);
    }
    System.out.println(HT);
}

}

yon342743761
yon342743761   2017.01.11 22:59

第一部分就是给定字符串:101_DE|102_AR|103_|104_JK|105_|106_KI|107_|108_BA
然后在控制台打印出:
arrayname[0][0] is 101
arrayname[0][1] is DE
…..

oyljerry
oyljerry   Ds   Rxr 2017.01.11 23:11

就是用string的split,先用\|做分割,然后各个元素再用下划线_做分割。就得到了各个数据。

yon342743761
yon342743761 第二次分要怎么分才能打印二维数组
11 个月之前 回复
xionglangs
xionglangs   Rxr 2017.01.12 12:19

public static void main(String[] args) {
Hashtable ht = new Hashtable();
String str="101_DE|102_AR|103_|104_JK|105_|106_KI|107_|108_BA";
String[] strs=str.split("\|");
for (int i = 0,len=strs.length; i < len; i++) {
String[] strss=strs[i].split("_");
if(strss.length==2){//103,107有问题啊,所以要判断是否为空
ht.put(strss[0], strss[1]);
}else{
ht.put(strss[0], "");
}
}
Iterator> it = ht.entrySet().iterator();
while(it.hasNext()){
Entry table = it.next();
System.out.println("key= " + table.getKey() + " and value= " + table.getValue());
}

}

图片说明,想取哪个值,只需要通过键值对取就可以了。

yon342743761
yon342743761 好!
11 个月之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!