如何在循环结束时连接数组
How to concatenate the arrays at the end of the loop
我在 Java 中创建了两个 class。主要的class'Main'用于将数组值传递给第二个class'Plan'。我的代码看起来像这样:
import java.util.ArrayList;
import java.utils.Arras;
public class Main {
public enum State {
A,
D,
H
};
Plan[] plan= new Plan[] {
new Plan(new State[]{State.A, State.A, State.A}),
new Plan(new State[]{State.A, State.D, State.H})};
}
我的另一个 class 'Plan' 看起来像这样:
import java.utils.Arrays;
public class Plan {
public static Main.State[] input;
public static Main.State[] output;
public static Main.State[] input_new = new Main.State[4];
this.input = input;
this.output = output;
this.input_new = input_new;
for(int i = 0; i < input.length; i++) {
input_new[i] = input[i];
}
}
现在在循环结束时,我想附加数组,以便它打印一个数组,即
A A A A D H.
我尝试使用 [this]/How can I concatenate two arrays in Java?) 方法,但它给我一个错误,说 'ArrayUtils' 无法解析。有人可以在这里指出我的错误吗?
提前致谢。
试试这个:
Stream.concat(Arrays.stream(arr1), Arrays.stream(arr2)).toArray();
添加 Apache Commons 依赖项? ArrayUtils
不是 JDK 的一部分。
如何执行此操作取决于您的构建工具。请 Google 如何将 JAR 依赖项添加到您正在使用的工具中。
class ArrayUtils
属于 Apache Commons Lang 图书馆。按照 link 获取 jar 并将其包含在您的 class 路径中。
对ArraysUtils
使用apache.commons
API
就像您与状态相关的答案一样,ArrayUtils 是 Apache Commons library. You can either add the dependency to your project, or use one of the manual approaches 的 class,它也与您使用的答案发布在同一主题中。
我在 Java 中创建了两个 class。主要的class'Main'用于将数组值传递给第二个class'Plan'。我的代码看起来像这样:
import java.util.ArrayList;
import java.utils.Arras;
public class Main {
public enum State {
A,
D,
H
};
Plan[] plan= new Plan[] {
new Plan(new State[]{State.A, State.A, State.A}),
new Plan(new State[]{State.A, State.D, State.H})};
}
我的另一个 class 'Plan' 看起来像这样:
import java.utils.Arrays;
public class Plan {
public static Main.State[] input;
public static Main.State[] output;
public static Main.State[] input_new = new Main.State[4];
this.input = input;
this.output = output;
this.input_new = input_new;
for(int i = 0; i < input.length; i++) {
input_new[i] = input[i];
}
}
现在在循环结束时,我想附加数组,以便它打印一个数组,即
A A A A D H.
我尝试使用 [this]/How can I concatenate two arrays in Java?) 方法,但它给我一个错误,说 'ArrayUtils' 无法解析。有人可以在这里指出我的错误吗?
提前致谢。
试试这个:
Stream.concat(Arrays.stream(arr1), Arrays.stream(arr2)).toArray();
添加 Apache Commons 依赖项? ArrayUtils
不是 JDK 的一部分。
如何执行此操作取决于您的构建工具。请 Google 如何将 JAR 依赖项添加到您正在使用的工具中。
class ArrayUtils
属于 Apache Commons Lang 图书馆。按照 link 获取 jar 并将其包含在您的 class 路径中。
对ArraysUtils
apache.commons
API
就像您与状态相关的答案一样,ArrayUtils 是 Apache Commons library. You can either add the dependency to your project, or use one of the manual approaches 的 class,它也与您使用的答案发布在同一主题中。