Split String []分成两个其他字符串[]

Nat*_*pos 2 java arrays string

我有一个String[]看起来像这样,第一个String是属性名称,第二个是值:

String[] full = { "property1", "value1", "property2", "value2", "property3", "value3" };
Run Code Online (Sandbox Code Playgroud)

我想把它String[]分成另外两个String[]这样的:

String[] properties = { "property1", "property2", "property3" };
String[] values = { "value1", "value2", "value3" };
Run Code Online (Sandbox Code Playgroud)

有没有办法以编程方式执行此操作?

PS:属性/值的数量Strings中full可能会发生变化

Jon*_*eet 12

最简单的方法就是:

String[] properties = new String[full.length / 2];
String[] values = new String[full.length / 2];
for (int i = 0; i < properties.length; i++)
{
    properties[i] = full[i * 2];
    values[i] = full[i * 2 + 1];
}
Run Code Online (Sandbox Code Playgroud)

很难看出你能以一种比这简单得多的方式做到这一点.您可能希望验证从full.length均匀开始.

建立两个数组而不是(比如说)a的任何理由Map<String, String>?