字符串形成为arrayList到arrayList

时间:2017-03-24 09:54:04

标签: java arraylist casting

我有以下字符串来自DB " [[" 22-1-2017; 10:00-19:00"],[" 22-1 -2017; 10:00-19:00"]]"

有没有一种简单的方法可以将字符串转换为arrayList?

提前致谢

3 个答案:

答案 0 :(得分:1)

如何使用像gson这样的JSON解析器?

String jsonArray = "[[\"22-1-2017;10:00-19:00\"],[\"22-1-2017;10:00-19:00\"]]";

Type listType = new TypeToken<List<List<String>>>(){}.getType();
List<List<String>> list = new Gson().fromJson(jsonArray, listType);

System.out.println(list);

输出:[[22-1-2017;10:00-19:00], [22-1-2017;10:00-19:00]]

答案 1 :(得分:0)

如果你想在字符串数组中使用日期,我有一个解决你的问题的方法:

ngAfterViewInit() {
   let inputField: HTMLElement = <HTMLElement>document.querySelectorAll('.dialog input')[0];
   inputField && inputField.focus();
}

如果您想要它而不使用引号,请使用

    String s = "[[\"22-1-2017;10:00-19:00\"],[\"22-1-2017;10:00-19:00\"]]";

    // first remove outter brackets
    s = s.substring(1, s.length());
    s = s.substring(0, s.length()-1);

    // Create a Pattern for getting everything within brackets
    Pattern r = Pattern.compile("\\[(.*?)\\]");
    // Now create matcher object.
    Matcher m = r.matcher(s);

    List<String> l = new ArrayList<>();
    while (m.find()) {
        l.add(m.group(1));
    }
    System.out.println(l);

希望这有帮助

编辑: 如果您使用

    Pattern r = Pattern.compile("\\[\"(.*?)\"\\]");

您根本不需要删除外部括号,因为它已经在以下内容中查找内容:

  • 用双引号打开方括号
  • 带方括号的双引号

答案 2 :(得分:0)

试试这个,

import numpy as np
from google.protobuf import timestamp_pb2

# numpy structure that mimics google.protobuf.Timestamp
Timestamp_t = np.dtype([('seconds', np.int64), ('nanos', np.int32)])

# populate numpy array with above structure
x_values_size = 3
x_values = np.empty((x_values_size,), dtype=Timestamp_t)
x_values['seconds'] = np.linspace(0, 100, num=x_values_size, dtype=np.int64)
x_values['nanos']   = np.linspace(0, 10, num=x_values_size, dtype=np.int32)

# copy data from numpy structured array to a descriptor-created Timestamp
for elem in np.nditer(x_values) :
    # destination protobuf structure (actually, part of some sequence)
    # try 1: this will actually change the type of 'ts'
    ts1 = timestamp_pb2.Timestamp()
    print(type(ts1)) # Timestamp as expected
    ts1 = elem
    print(ts1) # now a numpy.ndarray
    print(type(ts1))
    print(ts1.dtype)

    # try 2: assign member by member
    ts2 = timestamp_pb2.Timestamp()
    # fails with:
    # TypeError: array(0, dtype=int64) has type <class 'numpy.ndarray'>, but expected one of: (<class 'int'>,)
    ts2.seconds = elem['seconds']
    ts2.nanos = elem['nanos']
    print("-----")

产出= [22-1-2017; 10:00-19:00,22-1-2017; 10:00-19:00]