- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的目标是将数组 B[]
插入数组 A[]
中具有索引 K
的元素之后。
我不需要延长 A[]
,最后 5 个元素应该消失。
这就是我到目前为止所得到的。不要介意程序的开头,这只是我必须确定数组的一些额外要求。
例如:
如果我将 K
插入为 2
,则数组 A
为0 2 4 6 8 10 12 14 16 18 0 0 0 0 0
,数组 B 为 20 40 60 80 100
。最终的数组 A
应如下所示:
0 2 4 20 40 60 80 100 6 8 10 12 14 16 18
public static void main(String[] args) {
int A[] = new int [15];
int B[] = new int [5];
int K, i, j;
Scanner sc = new Scanner(System.in);
Random r = new Random();
for (i=10; i<=14; i++) {
A[i] = 0;
}
System.out.println("Matīss Lavrinovičs RDBD0 171RDB075");
System.out.print("K=");
if (sc.hasNextInt())
K = sc.nextInt();
else {
System.out.println("input-output error");
sc.close();
return;
}
sc.close();
if (K<0 || K>9) {
for (i=0; i<=9; i++)
A[i] = r.nextInt(50);
for (j=0; j<=4; j++)
B[j] = r.nextInt(100 - 50) + 50; }
else
for (i=0; i<=9; i++)
A[i] = i*K;
for (j=0;j<=4;j++)
B[j] = 10*(j+1)*K;
System.out.print("A: ");
i = 0;
while (i < 15) {
System.out.print(" " + A[i]);
if (i==14) System.out.println();
i = i + 1;
}
System.out.print("B: ");
j = 0;
while (j < 5) {
System.out.print(" " + B[j]);
j = j + 1;
}
do {
} while;
最佳答案
您可以使用System.arrayCopy
:
int[] a = new int[] { 0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 0, 0, 0, 0, 0 };
int[] b = new int[] { 20, 40, 60, 80, 100 };
int k = 2;
System.arraycopy(a, k + 1, a, k + 1 + b.length, a.length - b.length - k - 1);
System.arraycopy(b, 0, a, k + 1, b.length);
我们首先将索引 #2 b.length
之后的值复制到右侧(即 5)。然后我们将 b
的值复制到数组 a
的正确位置。
替代方法:
List<Integer> list = asList(a).subList(0, a.length - b.length);
list.addAll(k + 1, asList(b));
还有一个小辅助方法:
private static List<Integer> asList(int... ints) {
return IntStream.of(ints)
.boxed()
.collect(Collectors.toList());
}
<小时/>
您应该考虑以下因素:
{}
通常会导致代码中出现错误。您应该始终使用它们。关于java - 将一个数组中的数字插入到另一个数组的确切位置(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47184795/
通过多次搜索和pytorch文档本身,我可以发现在嵌入层内部有一个查找表,用于存储嵌入向量。我无法理解的是: 在这一层的培训期间究竟发生了什么? 权重是多少,以及这些权重的梯度是如何计算的? 我的直觉
当应用程序有大量数据(400M)要写入非阻塞套接字时,write() 返回EWOULDBLOCK 或EAGAIN 当发送缓冲区变满时。 当套接字被(e)轮询时,我有时会看到发送缓冲区中有 7M 空间(
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引起辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the he
我经常阅读一些编程语言对模块的支持(“一流”)(OCaml,Scala,TypeScript [?]),并且最近偶然发现了这样的答案,即在Scala的显着特征中引用模块作为一流公民。 我以为我很清楚模
我已经提交了一个自定义的开放图表故事,但它被拒绝并显示以下消息: 打开图表捐赠(行动类型)提交内容、操作、对象和使用说明必须以英文提交。您可以在应用程序面板的“本地化”选项卡中翻译操作和对象。 如果我
给定一个任意的 boolean 值列表,确定其中一个恰好为真的最优雅的方法是什么? 最明显的 hack 是类型转换:将 false 转换为 0,将 true 转换为 1,然后对它们求和,并返回 sum
这个问题在这里已经有了答案: When to use extern "C" in simple words? [duplicate] (7 个答案) 关闭 9 年前。 如果您想将此问题标记为重复问题
我是一名优秀的程序员,十分优秀!