java自定义数据结构碰到了问题,望高手指点
classMyLinkedList{/*一个链形链表对象,有一个头,身子,和尾,当只有一个元素时,他是头也是尾巴*/privateChainhead;privateCha...
class MyLinkedList{/*一个链形链表对象,有一个头,身子,和尾,当只有一个元素时,他是头也是尾巴*/
private Chain head;
private Chain tail;
private int length;
MyLinkedList(){}
public void setHead(Chain head){
this.head=head;
}
public Chain getHead(){
return head;
}
public void setTail(Chain tail){
this.tail=tail;
}
public Chain getTail(){
return tail;
}
public void setLength(int legth){
this.length=length;
}
public int getLength(){
return length;
}
public void add(Chain chain){
if(length==0){
head=chain;
tail=chain;
}else{
tail.setRightChain(chain);
chain.setLeftChain(tail);
tail=chain;
}
length++;
} public void listData_str(){
Chain current=head;
while(current!=null){
System.out.println(current.getData_str());
current=current.getRightChain();
}
}
}
class Chain{
private String data_str;
private int data_num;
private Chain leftChain;
private Chain rightChain;
Chain(){}
Chain(String data_str){
this.data_str=data_str;
}
Chain(int data_num){
this.data_num=data_num;
}
Chain(String data_str,int data_num){
this.data_str=data_str;
this.data_num=data_num;
}
public void setData_str(String data_str){
this.data_str=data_str;
}
public String getData_str(){
return data_str;
}
public void setData_num(int data_num){
this.data_num=data_num;
}
public int getData_num(){
return data_num;
}
public void setLeftChain(Chain leftChain){
this.leftChain=leftChain;
}
public Chain getLeftChain(){
return leftChain;
}
public void setRightChain(Chain rigthChain){
this.rightChain=rightChain;
}
public Chain getRightChain(){
return rightChain;
}
} 展开
private Chain head;
private Chain tail;
private int length;
MyLinkedList(){}
public void setHead(Chain head){
this.head=head;
}
public Chain getHead(){
return head;
}
public void setTail(Chain tail){
this.tail=tail;
}
public Chain getTail(){
return tail;
}
public void setLength(int legth){
this.length=length;
}
public int getLength(){
return length;
}
public void add(Chain chain){
if(length==0){
head=chain;
tail=chain;
}else{
tail.setRightChain(chain);
chain.setLeftChain(tail);
tail=chain;
}
length++;
} public void listData_str(){
Chain current=head;
while(current!=null){
System.out.println(current.getData_str());
current=current.getRightChain();
}
}
}
class Chain{
private String data_str;
private int data_num;
private Chain leftChain;
private Chain rightChain;
Chain(){}
Chain(String data_str){
this.data_str=data_str;
}
Chain(int data_num){
this.data_num=data_num;
}
Chain(String data_str,int data_num){
this.data_str=data_str;
this.data_num=data_num;
}
public void setData_str(String data_str){
this.data_str=data_str;
}
public String getData_str(){
return data_str;
}
public void setData_num(int data_num){
this.data_num=data_num;
}
public int getData_num(){
return data_num;
}
public void setLeftChain(Chain leftChain){
this.leftChain=leftChain;
}
public Chain getLeftChain(){
return leftChain;
}
public void setRightChain(Chain rigthChain){
this.rightChain=rightChain;
}
public Chain getRightChain(){
return rightChain;
}
} 展开
- 你的回答被采纳后将获得:
- 系统奖励15(财富值+成长值)+难题奖励30(财富值+成长值)
展开全部
Java代码
public static void message(){
// String package_header_signature = "hello";
// int file_num=8;
//
// for(int i=0;i<8;i++){
// int file_path_length="";
int file_size ="";
// String file_path="";
// }
// 只列了简单几个,原因都懂的!
// }
public static void test(String src,String target) throws Exception{
File file =new File(src);
//String target = "g://test.pkg";
File targetFile = new File(target);
if(!targetFile.exists()){
targetFile.createNewFile();
}
OutputStream os = new FileOutputStream(targetFile);
//write some message
String package_header_signature = "hello";
addByte(package_header_signature.getBytes(), os, 4);
//文件个数
int filesum=0;
//文件路径
List<String> strList = new ArrayList<String>();
filesum =listFlieSum(file,strList);
//加入文件个数
addByte(intToByte(filesum,1),os, 1);
for(int i=0;i<strList.size();i++){
File fileInfo = new File(strList.get(i));
String file_path = fileInfo.getAbsolutePath();
int file_path_length = file_path.length();
int file_size = getFileSize(fileInfo);
//加入文件路径长度
addByte(intToByte(file_path_length,1), os, 1);//文件路径的长度
//加入文件大小
addByte(intToByte(file_size,4), os, 4);//文件大小
//加入文件路径
addByte(file_path.getBytes(), os, file_path.length());//文件路径
}
listFile(file,os);
String crc32 = getFileCRCCode(targetFile);
byte[] crcbyte = crc32.getBytes();
os.close();
/**
* int to byte[] 支持 1或者 4 个字节
* @param i
* @param len
* @return
*/
public static byte[] intToByte(int i,int len) {
byte[] abyte=null;
if(len==1){
abyte = new byte[len];
abyte[0] = (byte) (0xff & i);
}else{
abyte = new byte[len];
abyte[0] = (byte) (0xff & i);
abyte[1] = (byte) ((0xff00 & i) >> 8);
abyte[2] = (byte) ((0xff0000 & i) >> 16);
abyte[3] = (byte) ((0xff000000 & i) >> 24);
}
return abyte;
}
public static int bytesToInt(byte[] bytes) {
int addr=0;
if(bytes.length==1){
addr = bytes[0] & 0xFF;
}else{
addr = bytes[0] & 0xFF;
addr |= ((bytes[1] << 8) & 0xFF00);
addr |= ((bytes[2] << 16) & 0xFF0000);
addr |= ((bytes[3] << 24) & 0xFF000000);
}
return addr;
}
public static void message(){
// String package_header_signature = "hello";
// int file_num=8;
//
// for(int i=0;i<8;i++){
// int file_path_length="";
int file_size ="";
// String file_path="";
// }
// 只列了简单几个,原因都懂的!
// }
public static void test(String src,String target) throws Exception{
File file =new File(src);
//String target = "g://test.pkg";
File targetFile = new File(target);
if(!targetFile.exists()){
targetFile.createNewFile();
}
OutputStream os = new FileOutputStream(targetFile);
//write some message
String package_header_signature = "hello";
addByte(package_header_signature.getBytes(), os, 4);
//文件个数
int filesum=0;
//文件路径
List<String> strList = new ArrayList<String>();
filesum =listFlieSum(file,strList);
//加入文件个数
addByte(intToByte(filesum,1),os, 1);
for(int i=0;i<strList.size();i++){
File fileInfo = new File(strList.get(i));
String file_path = fileInfo.getAbsolutePath();
int file_path_length = file_path.length();
int file_size = getFileSize(fileInfo);
//加入文件路径长度
addByte(intToByte(file_path_length,1), os, 1);//文件路径的长度
//加入文件大小
addByte(intToByte(file_size,4), os, 4);//文件大小
//加入文件路径
addByte(file_path.getBytes(), os, file_path.length());//文件路径
}
listFile(file,os);
String crc32 = getFileCRCCode(targetFile);
byte[] crcbyte = crc32.getBytes();
os.close();
/**
* int to byte[] 支持 1或者 4 个字节
* @param i
* @param len
* @return
*/
public static byte[] intToByte(int i,int len) {
byte[] abyte=null;
if(len==1){
abyte = new byte[len];
abyte[0] = (byte) (0xff & i);
}else{
abyte = new byte[len];
abyte[0] = (byte) (0xff & i);
abyte[1] = (byte) ((0xff00 & i) >> 8);
abyte[2] = (byte) ((0xff0000 & i) >> 16);
abyte[3] = (byte) ((0xff000000 & i) >> 24);
}
return abyte;
}
public static int bytesToInt(byte[] bytes) {
int addr=0;
if(bytes.length==1){
addr = bytes[0] & 0xFF;
}else{
addr = bytes[0] & 0xFF;
addr |= ((bytes[1] << 8) & 0xFF00);
addr |= ((bytes[2] << 16) & 0xFF0000);
addr |= ((bytes[3] << 24) & 0xFF000000);
}
return addr;
}
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |