如何在不知道数组大小的情况下初始化数组?

发布于 2024-10-09 09:27:38 字数 221 浏览 2 评论 0原文

我遇到一种情况,我必须对输入数组应用一个条件,并返回另一个数组作为输出,该数组根据过滤条件具有较小的大小。

现在的问题是我不知道过滤结果的大小,所以我无法用特定值初始化数组。我不希望它的大小很大,因为我正在使用 array.length;稍后。

一种方法是首先循环原始输入数组并设置一个计数器,然后使用该计数器长度进行另一个循环并初始化并填充该数组[]。但有没有办法只用一个循环来完成这项工作呢?

I have a situation, where I have to apply a criteria on an input array and reuturn another array as output which will have smaller size based upon the filtering criteria.

Now problem is I do not know the size of filtered results, so I can not initialize the array with specific value. And I do not want it to be large size will null values because I am using array.length; later on.

One way is to first loop the original input array and set a counter, and then make another loop with that counter length and initialize and fill this array[]. But is there anyway to do the job in just one loop?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

简单爱 2024-10-16 09:27:38

你不能...Java 中数组的大小始终是固定的。通常,您不使用数组,而是在此处使用 List 的实现 - 通常是 ArrayList,但还有许多其他可用的替代方案。

当然,您可以从列表创建一个数组作为最后一步 - 或者只是更改方法的签名以返回 List 开始。

You can't... an array's size is always fixed in Java. Typically instead of using an array, you'd use an implementation of List<T> here - usually ArrayList<T>, but with plenty of other alternatives available.

You can create an array from the list as a final step, of course - or just change the signature of the method to return a List<T> to start with.

夜无邪 2024-10-16 09:27:38

请改用LinkedList。然后,如果需要,您可以创建一个数组。

Use LinkedList instead. Than, you can create an array if necessary.

知你几分 2024-10-16 09:27:38

只需返回任何类型的列表即可。 ArrayList 就可以了,它不是静态的。

    ArrayList<yourClass> list = new ArrayList<yourClass>();
for (yourClass item : yourArray) 
{
   list.add(item); 
}

Just return any kind of list. ArrayList will be fine, its not static.

    ArrayList<yourClass> list = new ArrayList<yourClass>();
for (yourClass item : yourArray) 
{
   list.add(item); 
}
对风讲故事 2024-10-16 09:27:38

这是您班级的代码。但这也包含大量重构。请为每个添加一个而不是为。干杯:)

 static int isLeft(ArrayList<String> left, ArrayList<String> right)

    {
        int f = 0;
        for (int i = 0; i < left.size(); i++) {
            for (int j = 0; j < right.size(); j++)

            {
                if (left.get(i).charAt(0) == right.get(j).charAt(0)) {
                    System.out.println("Grammar is left recursive");
                    f = 1;
                }

            }
        }
        return f;

    }

    public static void main(String[] args) {
        // TODO code application logic here
        ArrayList<String> left = new ArrayList<String>();
        ArrayList<String> right = new ArrayList<String>();


        Scanner sc = new Scanner(System.in);
        System.out.println("enter no of prod");
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            System.out.println("enter left prod");
            String leftText = sc.next();
            left.add(leftText);
            System.out.println("enter right prod");
            String rightText = sc.next();
            right.add(rightText);
        }

        System.out.println("the productions are");
        for (int i = 0; i < n; i++) {
            System.out.println(left.get(i) + "->" + right.get(i));
        }
        int flag;
        flag = isLeft(left, right);
        if (flag == 1) {
            System.out.println("Removing left recursion");
        } else {
            System.out.println("No left recursion");
        }

    }

Here is the code for you`r class . but this also contains lot of refactoring. Please add a for each rather than for. cheers :)

 static int isLeft(ArrayList<String> left, ArrayList<String> right)

    {
        int f = 0;
        for (int i = 0; i < left.size(); i++) {
            for (int j = 0; j < right.size(); j++)

            {
                if (left.get(i).charAt(0) == right.get(j).charAt(0)) {
                    System.out.println("Grammar is left recursive");
                    f = 1;
                }

            }
        }
        return f;

    }

    public static void main(String[] args) {
        // TODO code application logic here
        ArrayList<String> left = new ArrayList<String>();
        ArrayList<String> right = new ArrayList<String>();


        Scanner sc = new Scanner(System.in);
        System.out.println("enter no of prod");
        int n = sc.nextInt();
        for (int i = 0; i < n; i++) {
            System.out.println("enter left prod");
            String leftText = sc.next();
            left.add(leftText);
            System.out.println("enter right prod");
            String rightText = sc.next();
            right.add(rightText);
        }

        System.out.println("the productions are");
        for (int i = 0; i < n; i++) {
            System.out.println(left.get(i) + "->" + right.get(i));
        }
        int flag;
        flag = isLeft(left, right);
        if (flag == 1) {
            System.out.println("Removing left recursion");
        } else {
            System.out.println("No left recursion");
        }

    }
再浓的妆也掩不了殇 2024-10-16 09:27:38

如果您使用的是 Java 8 或更高版本,则可以使用 Stream。这是仅提取 int[] 中偶数的示例。

static int[] evenFilter(int[] input) {
    return IntStream.of(input)
        .filter(i -> i % 2 == 0)
        .toArray();
}

public static void main(String args[]) throws IOException {
    int[] input = {3, 4, 22, 36, 49, 51};
    int[] output = evenFilter(input);
    System.out.println(Arrays.toString(output));
}

输出:

[4, 22, 36]

If you are using Java 8 or later, you can use Stream. This is an example of extracting only even numbers in int[].

static int[] evenFilter(int[] input) {
    return IntStream.of(input)
        .filter(i -> i % 2 == 0)
        .toArray();
}

public static void main(String args[]) throws IOException {
    int[] input = {3, 4, 22, 36, 49, 51};
    int[] output = evenFilter(input);
    System.out.println(Arrays.toString(output));
}

output:

[4, 22, 36]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文