VC中的结构体指针,作为某个函数的参数,或是返回值,在java中如何实现??

meetfuture 2005-11-22 09:58:22
VC中自定义结构体,如下:
typedef struct _listElement {
char type;
int start;
int end;
} listElement, *pListElement;
typedef struct _arrayList {
pListElement array;
int size;
int capacity;
} arrayList, *pArrayList;
函数如下:
pListElement listGet(pArrayList list, int idx) {
if (idx >= list->size || idx < 0)
return NULL;
return list->array + idx;
}

这里参数list是结构体指针,返回值类型也为结构体指针。
在java中该如何实现呢??
(我想这个结构体大概可以用类模拟,但是结构体arrayList中的成员pListElement array; 我又该怎么办呢?哎呀,一塌糊涂!!)
请大家指点!谢谢!
是否用ArrayList实现??(队列)
...全文
397 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
meetfuture 2005-11-23
  • 打赏
  • 举报
回复
大家早!
小问题又来了。
java中有没有类似vc中的memset函数啊?
ronny5 2005-11-22
  • 打赏
  • 举报
回复
返回一个collection 不就好了?
meetfuture 2005-11-22
  • 打赏
  • 举报
回复
兄弟姐妹们,大家过来帮个忙呀!
meetfuture 2005-11-22
  • 打赏
  • 举报
回复
那么结构体指针呢?如何用类实现?还有返回值为结构体指针??
zhenyang2002 2005-11-22
  • 打赏
  • 举报
回复
类来解决
meetfuture 2005-11-22
  • 打赏
  • 举报
回复
谢谢诸位了。
程序修改正在摸索中。
估计还会出现很多问题。
请大家做好被我骚扰的准备,呵呵!
pigo 2005-11-22
  • 打赏
  • 举报
回复

java的变量是在你需要的时候就声明,呵呵

你动手试一下就可以了,比c++代码简单多了

meetfuture 2005-11-22
  • 打赏
  • 举报
回复
那么我用这个list.add(e);的时候,这个list一定得在main中定义吗?还是可以作为一个类的成员变量,如果可以的话,那种更科学一些!请不要嫌我烦,我也不想折磨大家的神经,可是我没办法,只好请大家包容包容了!^-^
meetfuture 2005-11-22
  • 打赏
  • 举报
回复
那么这一块作为一个类封装起来,在我的主类中定义一个该类的实例,再做其他的处理,我总觉得这样做很困难。可能是我的思想还没转变过来吧。还是过程式的:(
meetfuture 2005-11-22
  • 打赏
  • 举报
回复
谢谢清秋梦冷,你的思路我看懂了。
我先给你说说这个程序的大致思想:
给定一个字符串,包括数字,字母,汉字等,首先将字符串分成一组一组队列,如:“234abc阿姐大t的打开”,那么分为5个队列分别存放“234”,“abc”,“阿姐大”,“t”,“的打开”,然后根据特定的规则将其中的队列整合在一起,如“234”与“abc”整合为一组,那么只剩下4个队列,然后分别对4个队列进行处理。
pigo 2005-11-22
  • 打赏
  • 举报
回复
import java.util.*;
//类声明
public class ListElement {
public char type;
public int start;
public int end;
//测试方法
public static void main(String[] args) {
char ctype='c';
int istart=0;
int iend=2;
List list=new ArrayList();
//添加
for (int i = 0; i < 20; i++) {
ListElement e=new ListElement();
e.type=ctype;
e.start=istart;
e.end=iend;
list.add(e);
}
//遍历
for (int i = 0; i <list.size(); i++) {
ListElement e=(ListElement)list.get(i);
System.out.println("type="+e.type+";start="+e.start+";end="+e.end);
}

}

meetfuture 2005-11-22
  • 打赏
  • 举报
回复
还有,这个ArrayList我具体应该在哪里定义呢?
上面的两个结构体转换为类后都不是主类。
meetfuture 2005-11-22
  • 打赏
  • 举报
回复
啊,但是我这个结构体里面的start和end都是很重要的参数呢,那么把他们整合到类listElement里面去?请指点,谢谢!
xdop 2005-11-22
  • 打赏
  • 举报
回复
在Java中使用链表,你只可能用 arraylist.get(index)去取元素,
一切都是类,你取得的元素也必然是个Object

所以你的 pListElement listGet(pArrayList list, int idx) 函数不存在了!

既然函数都不存在了,typedef struct _arrayList 结构体也没有存在的意义了!

世界清净了!
meetfuture 2005-11-22
  • 打赏
  • 举报
回复
主要是结构体数组的内存分配,这里是以队列的形式给出,java中如何实现呢
meetfuture 2005-11-22
  • 打赏
  • 举报
回复
static void listInit(pArrayList list) { //队列初始化
list->capacity = 20;
list->size = 0;
list->array = (pListElement)malloc(list->capacity * sizeof(listElement));
}
static void listAdd(pArrayList list, char type, int start, int end) { //队列添加
if (list->size == list->capacity) {
pListElement temp;
list->capacity *= 2;
temp = (pListElement)malloc(list->capacity * sizeof(listElement));
memcpy(temp, list->array, list->size * sizeof(listElement));
free(list->array);
list->array = temp;
}
list->array[list->size].type = type;
list->array[list->size].start = start;
list->array[list->size].end = end;
++list->size;
}
看看这两个子程序。
大家给详细解说解说吧。
是新手来着,问题比较幼稚,请各位见谅!
holy_phoenix 2005-11-22
  • 打赏
  • 举报
回复
自定义一个 class 表示 C++ 中的 struct,用这个 class 的实例来做参数。

62,629

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧