尝试打印已排序的字符串数组。因此,例如,如果我有一个字母从A到L的String数组,以便如何设置一个循环来获取输出。我似乎无法确定如何增加循环变量以获得正确的数字以按此顺序打印出数组。
所需的输出:
A D G J
B E H K
C F I L
String[] str = { "A", "B","C" ,"D" ,"E" ,"F" , "G" , "H" , "I" , "J" , "K", "L" };
答案 0 :(得分:0)
为此,您仍然需要从左到右,从上到下打印。没有简单的方法可以返回到上一行并在该行继续打印。
您应该能够计算表中的行数和列数。一旦知道了这些值,就可以执行以下操作:
0
的项目。D
)。1
开始,第二次迭代从2
开始迭代等)。答案 1 :(得分:0)
这是以所需格式打印的示例代码。它有两个循环-外循环打印行,内循环打印每一行的列。
File "/home/ubuntu/.pyenv/versions/cmle-1_12-py-3_5/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1334, in _do_call
return fn(*args)
File "/home/ubuntu/.pyenv/versions/cmle-1_12-py-3_5/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1317, in _run_fn
self._extend_graph()
File "/home/ubuntu/.pyenv/versions/cmle-1_12-py-3_5/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1352, in _extend_graph
tf_session.ExtendSession(self._session)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot assign a device for operation device_dummy_0/Initializer/random_uniform/RandomUniform: Could not satisfy explicit device specification '' because the node {{colocation_node device_dummy_0/Initializer/random_uniform/RandomUniform}} was colocated with a group of nodes that required incompatible device '/job:ps/task:0/device:CPU:0'
Colocation Debug Info:
Colocation group had the following types and devices:
IsVariableInitialized: CPU
Assign: CPU
Identity: CPU XLA_CPU
VariableV2: CPU
Mul: CPU XLA_CPU
Add: CPU XLA_CPU
Sub: CPU XLA_CPU
RandomUniform: CPU XLA_CPU
Const: CPU XLA_CPU
Colocation members and user-requested devices:
device_dummy_0/Initializer/random_uniform/shape (Const)
device_dummy_0/Initializer/random_uniform/min (Const)
device_dummy_0/Initializer/random_uniform/max (Const)
device_dummy_0/Initializer/random_uniform/RandomUniform (RandomUniform)
device_dummy_0/Initializer/random_uniform/sub (Sub)
device_dummy_0/Initializer/random_uniform/mul (Mul)
device_dummy_0/Initializer/random_uniform (Add)
device_dummy_0 (VariableV2) /job:ps/task:0/device:CPU:0
device_dummy_0/Assign (Assign) /job:ps/task:0/device:CPU:0
device_dummy_0/read (Identity) /job:ps/task:0/device:CPU:0
report_uninitialized_variables/IsVariableInitialized_1 (IsVariableInitialized) /job:ps/task:0/device:CPU:0
report_uninitialized_variables_1/IsVariableInitialized_1 (IsVariableInitialized) /job:ps/task:0/device:CPU:0
save/Assign_1 (Assign) /job:ps/task:0/device:CPU:0
[[{{node device_dummy_0/Initializer/random_uniform/RandomUniform}} = RandomUniform[T=DT_INT32, _class=["loc:@device_dummy_0"], dtype=DT_FLOAT, seed=0, seed2=0](device_dummy_0/Initializer/random_uniform/shape)]]```
输出:
public class PrintArray {
public static void main(String[] args) {
String[] str = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L"};
printArray(str, 3);
}
private static void printArray(String[] str, int numRows) {
for (int i = 0; i < numRows; i++) {
for (int j = 0; j < str.length; j += numRows) {
System.out.print(str[j + i] + " ");
}
System.out.println();
}
}
}