【问题标题】:Splitting an array at multiple points using an index point使用索引点在多个点拆分数组
【发布时间】:2014-04-29 05:29:56
【问题描述】:

我遇到过许多使用索引点将给定数组或数组列表拆分为两个数组或多个数组的示例。我试过了,他们工作得很好。我的问题是,是否可以根据索引点将数组拆分为多个块大小不同的数组?

例如:

array[] = {10,1,2,3,10,4,5,10,6,7,10,8,10};
index_value = 10;

那么输出应该是四个数组如:

arr1[] = {1,2,3}
arr2[] = {4,5}
arr3[] = {6,7}
arr4[] = {8}

当给定的数组是静态的且没有变化时,我能够拆分它们并显示结果。但是对于具有随机数和随机大小的数组实现相同的操作很困难。我需要将拆分值存储在许多子数组中,而不仅仅是打印值。

【问题讨论】:

  • 您需要数组,还是可以使用ArrayList<int> 之类的东西来存储您的数字?
  • 如果您使用索引作为拆分器值,那么随机值有什么影响??
  • @Sam arraylist 或 array any 很好..但我需要将拆分值作为单独的数组..因为现在我刚刚打印了一个小样本数组的值..
  • @Nullpointer 如果拆分值 = 10,并且如果数组是随机的,那么数组中 10 的数量也会变化..所以输出数组的数量是动态的..我不知道如何实施该

标签: java arrays random numbers


【解决方案1】:

我在 NetBeans 中尝试过,调试过,效果很好。

这些值将像 ArrayList 一样存储在其中,其中包含更多 ArrayList,如下所示:

  • arrayOfIntList.get(0) = > 1,2,3
    • arrayOfIntList.get(0).get(0) -> 1
    • arrayOfIntList.get(0).get(1) -> 2
    • arrayOfIntList.get(0).get(2) -> 3
  • arrayOfIntList.get(1) = > 4,5
    • arrayOfIntList.get(1).get(0) -> 4
    • arrayOfIntList.get(1).get(1) -> 5
  • arrayOfIntList.get(2) = > 6,7
    • arrayOfIntList.get(2).get(0) -> 6
    • arrayOfIntList.get(2).get(1) -> 7
  • arrayOfIntList.get(3) = > 8
    • arrayOfIntList.get(3).get(0) -> 8

    import java.util.ArrayList;

    public class Javaprueba {

    public static void main(String[] args) {
        int[] myarray = {10,1,2,3,10,4,5,10,6,7,10,8,10};
        int splitterInt = 10;
        int j = 0;
        int k = 0;
        ArrayList<Integer> tempArray = new ArrayList<Integer>();
        ArrayList<ArrayList<Integer>> arrayOfIntList = new ArrayList<ArrayList<Integer>>();

        for(int i=0; i<myarray.length; i++){

            if(myarray[i] != splitterInt){
                tempArray.add(j, myarray[i]);
                j++;                          
            }else{
                if(tempArray.size() > 0){
                    arrayOfIntList.add(k, tempArray);
                    k++;
                    tempArray = new ArrayList<Integer>();
                    j=0;
                }
            }

            if(i == myarray.length-1 && tempArray.size()>0){
                arrayOfIntList.add(k, tempArray);
            }
        }
    }
}

【讨论】:

  • 它也很有帮助..:)
【解决方案2】:

对于此解决方案,index_value 不必位于数组的开头或结尾:

int[] arr = new int[]{10,10,1,10,1,2,3,10,4,5,10,6,7,10,8,10,9,8,7,6,5,4,3,10,1,2,3,4,5,6,7,10,5,4,10,10};
int index_value = 10;

/** walk through the array and create the arraylist of arraylists */
ArrayList<ArrayList<Integer>> al = new ArrayList<ArrayList<Integer>>();
ArrayList<Integer> currAl = null;
for (int i=0; i < arr.length; i++) {
    if (arr[i] == index_value) {
        if (currAl != null && currAl.size() > 0)
            al.add(currAl);
        currAl = new ArrayList<Integer>();
    } else {
        if (currAl == null)
            currAl = new ArrayList<Integer>();
        currAl.add(arr[i]);
    }
}
if (arr[arr.length-1]!= index_value && currAl.size() > 0) {
    al.add(currAl);
}

/** print out the arraylist of arraylists */
for (int i=0; i < al.size(); i++) {
    currAl = al.get(i);
    for (int j=0; j < currAl.size(); j++) {
        System.out.print(currAl.get(j) + " ");
    }
    System.out.println();
}

【讨论】:

  • 我可以使用这个..它真的很好。现在我只需要对列表中的元素进行一些处理
【解决方案3】:

您可以尝试以下策略:查找标记边界,将遇到的每个段组成一个数组,然后将结果放入 Vector(例如)中。

package com.splitter;
import java.util.Vector;
public class Splitter {
/**
 * @param args
 */
public static void main(String[] args) {
    int[] array = {10, 1,2,3,10,4,5,10,6,7,10,8,10};
    Vector <int[]> result;

    result = split(array,10);
    for (int i = 0; i < result.size(); i++) {
        int[] split = result.get(i);
        System.out.println("Array " + i);
        for (int j=0; j<split.length; j++) {
            System.out.println(split[j]);
        }
    }
}

private static Vector<int[]> split(int[] array, int value) {
    Vector<int[]> result = new Vector<int[]>();
    int loc = 0;
    int start = 0;
    boolean isInside = false;
    while (loc < array.length) {
        if (array[loc] == value) {
            if (isInside) { // make array from start to here
                // make an array from start+1 to loc-1
                System.out.println(" .." + start + " " + loc + " v=" + array[loc] );

                int[] split = new int[loc - start - 1];
                for (int i = 0; i < split.length; i++) {
                    split[i] = array[start+1+i];
                }
                result.add(split);
                isInside = true;
            } 
            start = loc;
            isInside = true;
        }
        loc++;
    }
    return result;
}

}

【讨论】:

  • 我不确定向量概念。它对我来说是新的,但这个解决方案运行良好。我也可以用这个
  • Vector 只是一个简单的工件,它可以让您添加和查找元素,而无需知道您将拥有多少。可以使用其他数据结构来满足您的需要。如果有帮助,请在答案上标记接受。
【解决方案4】:

您可以通过使用 ArrayList 类来填写您的值来构建您的数组

array[] = {10,1,2,3,10,4,5,10,6,7,10,8,10};
index_value = 10;

ArrayList<ArrayList<int>> allLists = new ArrayList<ArrayList<int>>();
ArrayList<int> currentList;
for (int i = 0;i < array.length;i++)
{

    //in case your array doesn't contain the index_value at index 0
    //check for the null case
    if (array[i] == index_value || currentList == null)
    {
        currentList = new ArrayList<int>();
        allLists.add(currentList);
    }
    else
    {
        currentList.add(array[i]);
    }
}
if (allLists.size() > 0 && allLists.get(allLists.size() - 1).size() == 0)
{
    allLists.remove(allLists.size() - 1);
}

你应该有一个ArrayList,其中包含ArrayLists,其中包含你感兴趣的数字。但是,如果index_value连续重复两次,你最终会得到空列表(可以稍后删除)。这包括最终列表,如果它为空,我会自动将其删除(因为您的示例将 index_value 列为开头和结尾。

如果您希望输出在数组中,您可以在ArrayList 上调用.toArray()

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-01-19
    • 1970-01-01
    • 2019-09-05
    • 2014-11-17
    • 1970-01-01
    • 2017-09-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多