java如何设置变长的数组_java变长数组示例

package cn.CodeSnippet.snippets.core;

import java.io.File;

import java.io.FileInputStream;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

import java.io.PipedInputStream;

import java.io.PipedOutputStream;

import java.io.Serializable;

class SerialIntList implements Serializable {

protected int[] intArray = new int[8];

protected transient int length = 0;

public int get(int ind) {

if (ind >= length) {

throw new ArrayIndexOutOfBoundsException(ind);

} else {

return intArray[ind];

}

}

public void add(int i) {

if (intArray.length == length) {

resize(intArray.length * 2);

}

intArray[length++] = i;

}

protected void resize(int size) {

int[] newdata = new int[size];

System.arraycopy(intArray, 0, newdata, 0, length); // Copy array elements.

intArray = newdata; // Replace old array

}

private void writeObject(ObjectOutputStream out) throws IOException {

if (intArray.length > length) {

resize(length); // Compact the array.

}

out.defaultWriteObject(); // Then write it out normally.

}

@Override

public boolean equals(Object o) {

if (!(o instanceof SerialIntList)) {

return false;

}

SerialIntList that = (SerialIntList) o;

if (this.length != that.length) {

return false;

}

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

if (this.intArray[i] != that.intArray[i]) {

return false;

}

}

return true;

}

@Override

public int hashCode() {

int code = 1;

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

code = code * 997 + intArray[i];

}

return code;

}

}

public class Main {

static void store(Serializable o, File f) throws IOException {

ObjectOutputStream ostream = new ObjectOutputStream(new FileOutputStream(f));

ostream.writeObject(o);

ostream.close();

}

static Object load(File f) throws IOException, ClassNotFoundException {

ObjectInputStream instream = new ObjectInputStream(new FileInputStream(f));

return instream.readObject();

}

static Object deepclone(final Serializable o) throws IOException, ClassNotFoundException {

final PipedOutputStream pipeout = new PipedOutputStream();

PipedInputStream pipein = new PipedInputStream(pipeout);

Thread writer = new Thread() {

@Override

public void run() {

ObjectOutputStream out = null;

try {

out = new ObjectOutputStream(pipeout);

out.writeObject(o);

} catch (IOException e) {

} finally {

try {

out.close();

} catch (Exception e) {

}

}

}

};

// Start serializing and writing

writer.start();

ObjectInputStream in = new ObjectInputStream(pipein);

return in.readObject();

}

static class DataStructure implements Serializable {

String message;

int[] data;

DataStructure other;

@Override

public String toString() {

String s = message;

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

s += " " + data[i];

}

if (other != null) {

s += "\\n\\t" + other.toString();

}

return s;

}

}

public static void main(String[] args) throws IOException, ClassNotFoundException {

DataStructure structure = new DataStructure();

structure.message = "Java Code Geeks rocks!";

structure.data = new int[]{1, 2, 3, 4, 5};

structure.other = new DataStructure();

structure.other.message = "JavaCodeGeeks is the best!";

structure.other.data = new int[]{9, 8, 7};

System.out.println("Data: " + structure);

File f = new File("C:/Users/nikos7/Desktop/output2.txt");

System.out.println("Save to file");

Main.store(structure, f);

structure = (DataStructure) Main.load(f);

System.out.println("Read : " + structure);

DataStructure ds2 = (DataStructure) Main.deepclone(structure);

structure.other.message = null;

structure.other.data = null;

System.out.println("Deepcloning: " + ds2);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值