18

I need to convert from List<Object> to String[].

I made:

List<Object> lst ...
String arr = lst.toString();

But I got this string:

["...", "...", "..."]

is just one string, but I need String[]

Thanks a lot.


Its been a while, and I've now found a solution to this.

ForceBindIP

http://www.r1ch.net/stuff/forcebindip/

Download it, stick it in the Windows\System32 folder.

Find the GUID of the Network card you want to use (via RegEdit) and run this command...

C:\Windows\System32\ForceBindIP.exe {5CBB6FC0-3C9D-43C4-A166-0CB41387F7E8} "C:\Program Files (x86)\Mozilla Firefox\firefox.exe"

Bingo, firefox is bound only to the Wireless card.

Why? Because the wireless network at work is un-restricted, wired is restricted. So when I want to get to a blog entry that Google promises to solve all my problems, then its time to fire up Firefox.

4

7 に答える 7

27

リストをループして、String[].

String[] array = new String[lst.size()];
int index = 0;
for (Object value : lst) {
  array[index] = (String) value;
  index++;
}

リストの値が List である場合、これは;Stringを呼び出すのと同じくらい簡単です。lst.toArray(new String[0])

于 2013-02-08T13:08:49.820 に答える
14

toArray()を使用してオブジェクトの配列に変換した後、このメソッドを使用してオブジェクトの配列を文字列の配列に変換できます。

Object[] objectArray = lst.toArray();
String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);
于 2013-02-08T13:12:36.353 に答える
10

Java 8 には、次のようなストリームを使用するオプションがあります。

List<Object> lst = new ArrayList<>();
String[] strings = lst.stream().toArray(String[]::new);
于 2015-06-23T12:06:27.553 に答える
5

List<Object>のコレクションが含まれていることが確実な場合はString、おそらくこれを試してください。

List<Object> lst = new ArrayList<Object>();
lst.add("sample");
lst.add("simple");
String[] arr = lst.toArray(new String[] {});
System.out.println(Arrays.deepToString(arr));
于 2013-02-08T13:22:41.647 に答える
2

グアバの使用

List<Object> lst ...    
List<String> ls = Lists.transform(lst, Functions.toStringFunction());
于 2013-07-17T13:13:13.137 に答える
2

ここには便利な概念がたくさんあります:

List<Object> list = new ArrayList<Object>(Arrays.asList(new String[]{"Java","is","cool"}));
String[] a = new String[list.size()];
list.toArray(a);

文字列の配列を出力するためのヒント:

System.out.println(Arrays.toString(a));
于 2013-02-08T19:05:49.753 に答える