String[]、String、Object、Object[]的相互转换

String型数据转换成Object型数据:

String s = "test";

Object o = s;

Object型数据转换成String型数据:

Object obj="123456";
String str=obj.toString();

String[]数组转换成Object[]数组:

        String[] arr ={"abc","bcd","cde"};

        Object[] outputRow = new Object[arr.length];
        for(int i=0;i              outputRow[i] = arr[i];
        }
        for(Object o:outputRow) {
            System.out.println(o);
        }

Object[]数组转换为String[]数组:

String[] strs = new String[nameList.length];
for (int i = 0; i < obj.length; i++) {
strs[i] = nameList[i].toString();
}

 

字符数组char[]转换为String型数据:

  1. char[] data={'a','b','c'};

  2. String s=new String(data)

  3.  

String型数据转换为String[]数组:

 

String string = "abc,def,ghi";
    String[] strArr = string.split(",");//注意分隔符是需要转译
    for (int i = 0; i < strArr.length; i++) {
        System.out.println(strArr[i]);
    }


String[]数组转换为String型数据(只能通过循环,没有其他方法):

 

  1. String[] str = {"abc", "bcd", "def"};

  2. StringBuffer sb = new StringBuffer();

  3. for(int i = 0; i < str.length; i++){

  4. sb. append(str[i]);

  5. }

  6. String s = sb.toString();

List 转换成String[]数组:

List txtContent = readTxt("F:\\testinput.txt");
String[] arr = txtContent.toArray(new String[txtContent.size()]);

你可能感兴趣的:(String[]、String、Object、Object[]的相互转换)