---
title: "线性表合并"
author: "smallearth"
date: "2012-09-01T16:09:00Z"
tags: []
slug: "untitled"
draft: false
lastmod: "2013-11-30T20:10:44Z"
id: 1983330
url: "https://smallearth.iteye.com/blog/1983330"
---

类一

数组合并，但是数组又不能越界，为了节省内存，新建一数组用来合并数组，然后再赋值给要合并的数组

package org.se;  
//线性表合并  
public class arithMetic11 {  
public static int\[] la={1,5,4};//用数组代替线性表  
public static int\[] lb={4,6,45,56,7,8,9};  
public static int la\_len;//获取数组长度  
public static int lb\_len;

public arithMetic11() {  
Init();  
// TODO Auto-generated constructor stub  
}

public void Init() //初始化数据  
{  
la\_len=la.length;//获取数组长度  
lb\_len=lb.length;  
}  
public void ListInsert() //实现插入数据  
{  
int a;//用来储存lb中元素

for(int i=0;i&lt;lb.length;i++)  
{  
a=lb\[i];

if(!(LocateElem(a)))//判断la是否有等于a的元素  
{  
int \[] text=new int\[++la\_len]; //定义新的数组  
//System.out.println(la\_len); //text=la;如果这样赋值，则text的长度和la的长度一样  
for(int j=0;j&lt;la\_len-1;j++)  
{  
//System.out.println(la\_len);  
text\[j]=la\[j]; //将la复制给text  
}

text\[la\_len-1]=a;//将a放在新数组最后位置  
for(int x=0;x&lt;la\_len;x++)  
{  
GetElem(text);  
//System.out.println(la\_len);  
System.out.println("la\["+x+"]..."+la\[x]);  
}  
}  
}  
}  
static boolean LocateElem(int e)//判断la是否有等于a的元素  
{  
boolean b=true; //判断la是否有等于a的元素  
for(int j=0;j&lt;la.length;j++)  
{  
if(e!=la\[j])  
{  
b=false;  
}  
else  
{  
b=true;  
}  
}  
return b;  
}

public void GetElem(int\[] p) //将新数组再赋值给la  
{  
la=null;  
la=p;  
}  
}

类二

package org.se;

public class test {

static arithMetic11 am;

public static void main(String\[] args) {  
am=new arithMetic11();  
am.ListInsert();  
// TODO Auto-generated method stu  
}  
}