遇到一个数组问题,大家帮忙看一下!

nccxl 2006-01-25 10:54:10
我的问题是:我有一个数组A,可能里面的内容很多,假设有1000的长度。我现在希望取他第100个开始,后面的全部内容。

我知道用新建另一个数组B来做会比较方便,但是我的A内容实在太多了,如果在看几乎同样大的B很可能会出现内存不足的错误。

所以我希望是用数组A来完成这个种操作,不知道大家可有办法?
...全文
904 45 打赏 收藏 转发到动态 举报
写回复
用AI写文章
45 条回复
切换为时间正序
请发表友善的回复…
发表回复
cozmic 2006-02-16
  • 打赏
  • 举报
回复
呵呵,有点意思,既然是由其他流决定长度的,那就在读取的过程中判断从那里开始到那里结束不就搞定了?
如果无法判断的话,建议用一个固定大小的缓存如256B,在本地检测其是否有效,如果无效的话抛弃,有效的话保留 ( String.append())
nccxl 2006-02-14
  • 打赏
  • 举报
回复
非常感谢楼上的各位,过年都有人回复,太感动了。我马上结帖,祝大家新年快乐,心想事成!
kongxiangzhe 2006-02-03
  • 打赏
  • 举报
回复
public static void arraycopy(Object src,
int srcPos,
Object dest,
int destPos,
int length)
我想这个可以帮你解决!!
xj321 2006-02-02
  • 打赏
  • 举报
回复
转换成string行不 然后再用BufferString类不就可以了???我菜 别仍我
已经存在 2006-02-02
  • 打赏
  • 举报
回复
对啊,建议用ArrayList好了,操作起来不用这么复杂了!
racewind 2006-01-31
  • 打赏
  • 举报
回复
上面的程序有点问题,这个就是了:



class Test
{
int m[] = new int[100];
public int[] math(int x)
{
for(int i=0;i<m.length;i++)
{
m[i] = i;
}
for(int i=0;i<m.length-x;i++)
{
m[i] = x;
x++;
}
return m;
}
Test()
{
math(33);
for(int i=0;i<m.length;i++)
{
System.out.println(m[i]);
}
}
public static void main(String args[])
{
new Test();
}
}
racewind 2006-01-31
  • 打赏
  • 举报
回复



class Test
{
int m[] = new int[1000];
public int[] math(int x)
{
for(int i=0;i<m.length;i++)
{
m[i] = i;
}
for(int i=0;i<m.length;i++)
{
m[i] = x;
x++;
}
return m;
}
Test()
{
math(100);
for(int i=0;i<m.length;i++)
{
System.out.println(m[i]);
}
}
public static void main(String args[])
{
new Test();
}
}
racewind 2006-01-31
  • 打赏
  • 举报
回复
可以用ArrayList啊!
mistapostle 2006-01-27
  • 打赏
  • 举报
回复
数组好象没什么办法了,用LIST之类的COLLECTION只会更消耗资源
还是用ByteBuffer吧,创建消耗小,对于楼主的问题只要在100的地方MARK一下,然后REWIND()就好了
universe01 2006-01-27
  • 打赏
  • 举报
回复
知道beginIndex / endIndex不就可以了吗?
不用copy的!

但是这里有一个问题,不用的部分内存不能释放啊!


725137 2006-01-26
  • 打赏
  • 举报
回复
我提供思路.代码太多.难得写..
动态创建一小数组.这个小数组是A中的一部分数据..然后写成一个类.这个类可以创建多个小数组..用的时候需要用哪部分的数据。就调用这个类中的创建哪部分小数组的方法.
nccxl 2006-01-26
  • 打赏
  • 举报
回复
感谢gtlang78大哥,你的代码很好。不过我现在应该是不能让Array和List同时存在与内存中的。因为我的Array实在太大,如果转成List那就变成双份的内存使用量了。
gtlang78 2006-01-26
  • 打赏
  • 举报
回复
帮你写了一个包装的类

import java.util.Iterator;

public class CharList implements Iterable<Character> {
private char[] charArray = null;

private int fromIndex;

private int toIndex;

public CharList(char[] charArray) {
this(charArray, 0, charArray.length - 1);
}

public CharList(char[] charArray, int fromIndex, int toIndex) {
if (fromIndex < 0) {
throw new ArrayIndexOutOfBoundsException("fromIndex < 0");
}
if (toIndex > charArray.length - 1) {
throw new ArrayIndexOutOfBoundsException("toIndex > " + (charArray.length - 1));
}

this.charArray = charArray;
this.fromIndex = fromIndex;
this.toIndex = toIndex;
}

public int length() {
return toIndex - fromIndex + 1;
}

public char getCharAt(int index) {
return charArray[index + fromIndex];
}

public void setCharAt(int index, char c) {
charArray[index + fromIndex] = c;
}

public CharList subList(int fromIndex, int toIndex) {
if (fromIndex < 0) {
throw new ArrayIndexOutOfBoundsException("fromIndex < 0");
}
if (toIndex > length() - 1) {
throw new ArrayIndexOutOfBoundsException("toIndex > " + (length() - 1));
}

return new CharList(this.charArray,
this.fromIndex + fromIndex,
this.fromIndex + toIndex);
}

public Iterator<Character> iterator() {
return new Iterator<Character>() {
int cursor = 0;

public boolean hasNext() {
return cursor != length();
}

public Character next() {
return new Character(getCharAt(cursor++));
}

public void remove() {
throw new UnsupportedOperationException("Remove operation not supportted.");
}
};
}

public static void main(String[] args) {
char[] charArray = "0123456789ABCDEF".toCharArray();
CharList charList = new CharList(charArray);
for (int i = 0; i < charList.length(); i++) {
System.out.print(charList.getCharAt(i) + " ");
}
System.out.println();

CharList subList = charList.subList(3, 10);
for (int i = 0; i < subList.length(); i++) {
System.out.print(subList.getCharAt(i) + " ");
}
System.out.println();

for (char c : subList.subList(2, 4)) {
System.out.print(c + " ");
}
System.out.println();
}
}
sheep219 2006-01-26
  • 打赏
  • 举报
回复
可以啊,用循环就可以了。
nccxl 2006-01-26
  • 打赏
  • 举报
回复
问题回传后的处理是封装好了的:( 所以比较头痛。
cuilichen 2006-01-26
  • 打赏
  • 举报
回复
使用全局变量。
水晶平衡木 2006-01-26
  • 打赏
  • 举报
回复
是的,不过看样子目前能作的只有这些了。
那还是新建一个数组拷贝后返回吧……Java中没有可变数组……
didoleo 2006-01-26
  • 打赏
  • 举报
回复
System.arraycopy()方法可以对同一个数组进行操作,这个方法是一个native方法,如果对同一个数组进行操作时,会首先把从源部分拷贝到一个临时数组,在把临时数组的元素拷贝到目标位置。

Object[] a=new Object[1000];
System.arraycopy(a,100,a,0,a.length-100);
nccxl 2006-01-26
  • 打赏
  • 举报
回复
非常感谢楼上的几位。
我之所以希望能截取是因为我要传回这个字符数组,如果按boltzjf的做法,那我还必须传回结束的长度位置了。这样就比较麻烦了。
sjjf 2006-01-26
  • 打赏
  • 举报
回复
为什么不把一个数据分成几个数组来做啊?
一个存储1000个的数组
分成来两个, 100 和 900的。
放到一个容器里面。



加载更多回复(25)

62,616

社区成员

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

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