我有一个包含一些值的数组和一个包含两个数组的函数。我想要做的是拆分数组,以便最后一个元素不在数组中,所以我有两个数组,一个包含除最后一个元素之外的所有原始元素,另一个只有一个元素 - 最后一个元素。在我将它传递给函数之后,我想使用原始数组,但这次将第二个最后一个元素放入一个单独的数组中,依此类推。我想这样做五次。以下将更好地解释它:
int[] val = {25,50,66,75,100,1000,5000,10000,25000};
for (int i=0; i<5; i++){
//Need to split the array and then pass it to the function here
}
public void evaluate(int[] train, int[] test){
....
}
因此,例如在第一次迭代中,我想从数组中删除/拆分25000并将其放入另一个数组中,然后通过该函数传递两个数组:
第一个数组现在有{25,50,66,75,100,1000,5000,10000}
第二个数组现在有{25000}
在下一次迭代中,我想现在拆分/删除10000(25000现在回到数组中):
所以第一个数组现在有{25,50,66,75,100,1000,5000,25000}
第二个数组现在有{10000}
所以基本上它是从底部开始并且一直向上,但只有5次。
答案 0 :(得分:1)
最简单的方法是从使用数组切换到使用List<Integer>
。然后,您可以使用subList
方法构造作为原始数组的子序列的数组。
如果您坚持使用数组,则有两种选择:
以下是一些代码,用于说明如何使用List<Integer>
:
// autobox each value as an Integer:
List<Integer> vals = Arrays.asList(
new Integer[] {25,50,66,75,100,1000,5000,10000,25000});
final int len = vals.length();
for (int i=0; i<5; i++){
evaluate(vals.subList(0, i), vals.subList(i, len));
}
public void evaluate(List<Integer> train, List<Integer> test){
....
}
答案 1 :(得分:1)
使用Arrays.copyOf(..)方法查看Arrays API。
newArray = Arrays.copyOf(oldArray, oldArray.length-1)
也许这段代码可以帮到你。
int[] val = { 25, 50, 66, 75, 100, 1000, 5000, 10000, 25000 };
int[] firstArray = new int[val.length-1];
int[] SecondArray = new int[1];
//iterates the whole array set to 5 if needed
for (int n = 0; n < val.length; n++) {
SecondArray[0] = val[val.length-n-1];
for(int x = 0, firstArrayCounter= 0; x < val.length; x++){
if(val[x]!=SecondArray[0]){
firstArray[firstArrayCounter] = val[x];
firstArrayCounter++;
}
}
//prints what is in the arrays
for (int i = 0; i < firstArray.length; i++)
System.out.print(firstArray[i] + " ");
System.out.println("\n"+SecondArray[0]);
}
祝你好运!
答案 2 :(得分:1)
我不知道你到底想要做什么,但这似乎非常适合功能性编程语言。无论如何,我们可以在java中使用数组:
在你的循环中,从1到5包括你可能会有类似的东西:
for (int i=1; i<=5; i++){
int[] train = new int[val.length-i];
System.arraycopy( val, 0, train, 0, train.length-1 );
int test = new int[1];
test[0] = val[val.length-i];
evaluate(train,test);
}
答案 3 :(得分:1)
我将使用的算法是:
单个副本为O(n)
,并且5个迭代中的每个都具有进行交换的恒定时间操作。记忆也是O(n)
。
答案 4 :(得分:1)
你可以使用Apache commons的ArrayUtils,代码示例如下:
private static int[] val = { 25, 50, 66, 75, 100, 1000, 5000, 10000, 25000 };
private static int[] test = {};
public static void evaluate(int[] train, int[] test) {
for (int i = 0; i < train.length; i++) {
System.out.print(train[i] + ",");
}
System.out.println("");
for (int i = 0; i < test.length; i++) {
System.out.print(test[i] + ",");
}
System.out.println("");
System.out.println("-----");
}
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
if (!ArrayUtils.isEmpty(test))
ArrayUtils.remove(test, 0);
evaluate(ArrayUtils.remove(val, val.length - 1 - i), ArrayUtils.add(test, val[val.length - 1 - i]));
}
}
答案 5 :(得分:1)
预先创建两个数组,并在每次迭代后将一个元素交换到测试数组中。这比一直分配新数组要快。
int[] val = {25,50,66,75,100,1000,5000,10000,25000};
// create the destination arrays:
int[] train = new int[val.length-1];
int[] test = new int[1];
// initialize the arrays:
test[0] = val[val.length-1];
for (int i = 0; i < val.length-1; ++i)
{
train[i] = val[i];
}
int timesToIterate = 5;
for (int iteration = 0; iteration < timesToIterate; ++iteration)
{
evaluate(train, test);
int i = train.length-1-iteration;
if (i >= 0) // don't swap if this is the last element in the array
{
int tmp = test[0];
test[0] = train[i];
train[i] = tmp;
}
}
使用示例数据,传递给evaluate函数的数组是:
{25 50 66 75 100 1000 5000 10000 } {25000}
{25 50 66 75 100 1000 5000 25000 } {10000}
{25 50 66 75 100 1000 10000 25000 } {5000}
{25 50 66 75 100 5000 10000 25000 } {1000}
{25 50 66 75 1000 5000 10000 25000 } {100}