【问题标题】:How to traduce ArrayList.add from java to C?如何将 ArrayList.add 从 java 转换为 C?
【发布时间】:2020-11-16 18:33:36
【问题描述】:

我想知道你是否可以帮我解决这个问题。

我有一个循环读取一些数字。

我想初始化一个 malloc,它将这些数字中的每一个添加到一个数组中,但我们不知道 malloc 的大小。

你能帮我解决这个问题吗?

我想做这样的事情。我知道如何用 Java 做,但我想用 C 做:

int[] mark = {1, 2, 3, 4}; //how to do if we don't know the size of mark?

ArrayList<Integer> arr = new ArrayList;
for loop{
arr.add(mark[i]); //so, for each loop the malloc adds mark[i]. We don't care about the size of the malloc
}

谢谢!

【问题讨论】:

  • 好吧,为什么不开始写c 看看会发生什么?在java中写一个关于c的问题很奇怪
  • 因为我完全了解 Java。我是C新手,我不知道该怎么做。对我来说,如何在 java 中做到这一点是 100% 合乎逻辑的,但我在 C 中一无所知
  • @user13425812 通常你从十个元素开始(比如malloc(10 * sizeof(int))),然后编写一个函数来添加到分配的内存范围中的正确位置并跟踪你有多少元素放在那里,当它满了的时候你realloc它,比方说,它的当前大小加倍。
  • 如果您阅读 ArrayList 的 java 代码,您会发现它不会在每次添加新元素时分配新内存。它使用一个变量,如顶部,用于控制数组的大小(增长时,收缩时)。你可以在 C 中做同样的事情
  • @user13425812 你搞定了吗?

标签: java c loops arraylist malloc


【解决方案1】:

Java(7) ArrayList 实现:https://github.com/openjdk-mirror/jdk7u-jdk/blob/master/src/share/classes/java/util/ArrayList.java

如果您查看 add 方法,您会注意到每次调用此方法都会检查数组的容量(如果已满)。

如果满了,一定要增长(一般是实际大小的50%),行:207,它使用value>>1,也一样作为值/2

之后,它只是将元素放入数组中。

这段代码可能会对你有所帮助。

#ifndef MY_ARRAY_H
#define MY_ARRAY_H

#include <stdlib.h>
#include <stdio.h>

struct array {
    int* elements;
    unsigned int max_elements;
    unsigned int top;
};

void add(struct array* ptr_array, int element) {    
    ptr_array->top++;
    if(ptr_array->top == ptr_array->max_elements) {
        puts("Reached the maximum size. \tIncreasing ...");
        // grow, in java array list, the grow is 50% of the current size, 
        unsigned int new_max = ( ptr_array->max_elements / 2 ) + ptr_array->max_elements;
        printf("Current size: %d\n", ptr_array->max_elements);
        printf("New max size: %d\n", new_max);

        // number of positions multiplying the size of bytes that an integer uses in memory
        int* new_ptr = (int*) realloc((void*) ptr_array->elements, new_max * sizeof(int));
        ptr_array->elements = new_ptr;
        ptr_array->max_elements = new_max;
    }
    printf("Top: %d\n", ptr_array->top);
    ptr_array -> elements[ptr_array->top] = element; 
}

struct array init(void) {
    struct array arrayList;
    int initial_size = 2; // as java, the default init size is 2.
    arrayList.elements = (int*) malloc(initial_size * sizeof(int));
    arrayList.max_elements = initial_size;
    arrayList.top = -1;
    return arrayList;
}


int main(void) {
    struct array arrayList = init();

    int max = 25;

    for(int index = 0; index < max; index++) {
        add(&arrayList, index + 100);
    }
    
    for(int index = max -1; index > 0; index--) {
        printf("%d, ", arrayList.elements[index]);
    }
    printf("%d.\n", arrayList.elements[0]);
    
    return 0;

}

#endif

Ps,我也是一名 Java 程序员。 :)

【讨论】:

  • 我必须说-&gt; 周围的空格是一种非常不寻常的约定。
  • 感谢您的建议。由于我来自 Java,我不擅长 C 约定。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-19
  • 1970-01-01
  • 1970-01-01
  • 2012-07-31
  • 2013-08-20
  • 2020-04-02
相关资源
最近更新 更多