public class C {
public static void main(String[] args) {
int[] a1 = { 20, 40, 50, 70, 90 };
int[] a2 = { 10, 30, 40, 60, 80 };
int[] a3 = new int[a1.length + a2.length];
for (int i = 0; i < a1.length; i++) {
a3[i] = a1[i];
}
for (int j = 0; j < a2.length; j++) {
a3[a3.length - j - 1] = a2[a2.length - j - 1];
}
for (int i : a3) {
System.out.println(i);//排序前输出a3
}
for (int i = 1; i < a3.length; i++) {
if (a3[i - 1] > a3[i]) {
int tmp = a3[i];
int j = i - 1;
do {
a3[j + 1] = a3[j];
j--;
} while (j >= 0 && tmp < a3[j]);//当j>=0并且 当前值大于数据中j位置的值时移动
a3[j + 1] = tmp; //插入排序值
}
}
System.out.println("after sort:")
for (int i : a3) {
System.out.println(i);//排序后输出a3
}
System.out.println();
}
}
单类型,多个数组合并,可以参考我提供的代码。
import java.util.Arrays;
public class C {
/**
* @param args
*/
public static void main(String[] args) {
int[] a1 = { 20, 40, 50, 70, 90 };
int[] a2 = { 10, 30, 40, 60, 80 };
int[] a3 = new C().mergeArrays(new Object[] { a1, a2 });//任意多个数组都可以
Arrays.sort(a3);// 升序排序
for(int a:a3){
System.out.println(a);
}
}
/**
* 合并数组
* 先求得大数组的总长度
* @return 合并后的数组
*/
public int[] mergeArrays(Object[] obj) {
int[] new_a3 = new int[getArraysLength(obj)];
int index = 0;
for(Object objs:obj){
for(int a:(int[])objs){
new_a3[index] = a;
index++;
}
}
return new_a3;
}
/**
* 求需要合并数组的总长度
* @param obj
* @return 总长度,-1:obj为无效数组
*/
public int getArraysLength(Object[] obj) {
int length = 0;
if (obj != null && obj.length > 0) {
for (Object objs : obj) {
length = length+((int[]) objs).length;
}
return length;
}
return -1;
}
}
public static void main(String[] args) {
int[] a = { 1, 6, 4, 9, 8, 7 }, b = { 3, 4, 5, 10 };
// 将 a,b合并
int[] c = new int[a.length + b.length];
System.arraycopy(b, 0, c, 0, b.length);
System.arraycopy(a, 0, c, b.length, a.length);
// 排序
Arrays.sort(c);
// 打印校对
for(int i = 0;i<c.length;i++)
System.out.print(c[i] + " ");
}
分为两步:
1.连接两个数组.
2.清除重复的元素.
import java.util.Arrays;
public class Combine{
public static void main(String[] args){
int a[]={1,2,3,4,5};
int b[]={4,5,6,7,8};
int temp[]=new int[a.length+b.length];
//连接两个数组
for(int i=0;i<a.length;i++){
temp[i]=a[i];
}
for(int i=0;i<b.length;i++){
temp[a.length+i]=b[i];
}
//连接数组完成,开始清除重复元素
int size=temp.length;
for(int i=0;i<temp.length;i++){
if(temp[i]!=-1){
for(int j=i+1;j<temp.length;j++){
if(temp[i]==temp[j]){
temp[j]=-1;//将发生重复的元素赋值为-1
size--;
}
}
}
}
int[] result=new int[size];
for(int i=0,j=0;j<size && i<temp.length;i++,j++){
if(temp[i]==-1){
j--;
}
else{
result[j]=temp[i];
}
}
//打印结果
System.err.println(Arrays.toString(result));
}
}
/**
* 将两个数组连接起来
*
* @param first:
* 第一个数组
* @param second:
* 第二个数组
* @return :返回连接起来的新数组
*/
public static int[] connect(int[] frist, int[] second) {
int x [] = new int [frist.length + second.length];
int c = 0;
for(int i = 0 ; i < frist.length; i ++){
x[i] = frist[i];
}
for(int i = frist.length; i < x.length; i ++){
x[i] = second[c];
c ++;
}
return x;
}
public static void main(String[] args) {
int a [] = {1,2,3,4,5};
int b [] = {4,5,6,7,8};
int x [] = new int [a.length + b.length];
x = Arrays.connect(a,b);
for(int i = 0;i < a.length + b.length ;i ++){
System.out.println(x[i]);
}
}
a[1, 3, 4 , 6,8]与b[1 , 4,6,7]数组比较除去a[]中数据与b[]中相同的得到a[3,8]其java代码怎么写
Vector<Integer>A=new Vector<Integer>();
for(int v:a){
A.add(new Integer(v));
}
Integer B;
for(int v:b){
B=new Integer(v);
if(A.contains(B)){
A.remove(B);
}
}
Integer[] A1=A.toArray(Integer[0]);
a=new int[A1.length];
for(int i=A1.length-1;i>=0;i--){
a[i]=A1[i];
}