24、合并两个无序数组,并把合并后的升序数组写入到a.txt中,降序数组写入到b.txt中。要求去掉重复元素

写这个程序之前,先总结一下最近遇到的笔试题的小问题:

 

1、Math.round()这个函数,见过好多次,可是笔试的时候,还是拿不准答案。。下面贴一下自己的实验~

math.round(-10.6)=-11。 但是math.round(-10.5)返回值为什么是-10 而不是-11 不应该四舍五吗??

 

public class TestRound {
	public static void main(String args[]){
		System.out.println(Math.round(11.4));//11
		System.out.println(Math.round(-11.4));//-11
		
		System.out.println(Math.round(11.5));//12
		System.out.println(Math.round(-11.5));//-11
	}
}


我们看看源代码:

看看源代码

public static long round(double a) {
	return (long)floor(a + 0.5d);
    }


可以看到这个round()方法实现的是:就是括号内的数+0.5之后,向下取值(取第一个比它小的整数,负数时候要特别小心),比如:round(3.4)就是3.4+0.5=3.9,向下取值是3,所以round(3.4)=3; 那么round(-10.5)就是-10.5+0.5=-10,向下取值就是-10,所以round(-10.5)=-10

 

 

2、关于equals()也是看过好多和==的比较,但是还是有未注意到的地方

比如:

 

3、abstract方法有{}吗?

没有,抽象方法的实现:

abstract void f();

 

一个抽象类中可以没有抽象方法;

也可以有一个或多个抽象方法;

一个类A继承一个抽象类,必须实现全部抽象方法,否则A 依然必须是abstact的

 

 

4、接口的修饰符?以下哪些不可以使用??

public  protected private static final

接口本身只能使用public 修饰

接口中的字段和方法,默认的:

    字段默认不写,被解释为:public static final

    方法默认不写,被解释为:public abstract

 

5、JAVA的变量命名规则?

java变量名,最多包含:字母、数字,下划线,美元符号。并且,数字不能开头

java中的变量名主要遵循五个规则:
  
  1、只能以字母、“_”或“$”符号作为变量名开头。
  
  2、变量中可包含数字,但不能以数字开头
  
  3、除了“_”和“$”符号以外,变量中不能包含其他特殊字符。
  
  4、不能用class、int、String、public等java关键字做为变量名。
  
  5、在java中变量名严格区分大小写,例如:name和Name就是两个不同的变量。

6、int a[] = new int(25);

求a[24]?

应该是0

 

7、构造方法,不能被继承,所以不能被重写。但是能被重载

 

8、合并两个无序数组

方法1、使用System.arraycopy()复制到一个新的数组中。这种方法不能去掉重复元素,并且,合并的数组无序。

import java.util.Arrays;


public class ArrayCopy {

	public static void main(String args[]){
		int a[]={9,5,17,3};
		int b[] = {8,17,4,6};
		copyTest(a,b);
	}
	
	public static void copyTest(int a[],int b[]){
		int temp[] = new int[a.length+b.length];
		System.arraycopy(a, 0, temp, 0, a.length);
		System.arraycopy(b, 0, temp, a.length, b.length);
		System.out.println(Arrays.toString(temp));//结果为:[9, 5, 17, 3, 8, 17, 4, 6]
	}
}

 

 

改进,方法2、使之去掉重复元素

import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator;


public class ArrayCopy {

	public static void main(String args[]){
		int a[]={9,5,17,3};
		int b[] = {8,17,4,6};
		copyTest(a,b);
	}
	
	public static void copyTest(int a[],int b[]){
		//int temp[] = new int[a.length+b.length];
		HashSet<Integer> hs = new HashSet();//使用set可以自动去掉重复的元素
		//int i;
		//int j;
		int len1 = a.length;
		int len2 = b.length;
		for(int i=0, j=0;i<len1&&j<len2;i++,j++){//使用for循环进行控制
		    hs.add(a[i]);//添加元素
		    hs.add(b[j]);//添加元素
		}
		//使用集合进行输出
		Iterator<Integer> it = hs.iterator();
		while(it.hasNext()){
			System.out.println(it.next() + " ");//结果为:17 3 4 5 6 8 9
			
			}
	}


方法3、使合并后的数组有序

import java.util.Arrays;


public class ArrayCopy {

	public static void main(String args[]){
		int a[]={9,5,17,3};
		int b[] = {8,17,4,6};
		copyTest(a,b);
	}
	
	public static void copyTest(int a[],int b[]){
		int temp[] = new int[a.length+b.length];
		System.arraycopy(a, 0, temp, 0, a.length);
		System.arraycopy(b, 0, temp, a.length, b.length);
		System.out.println(Arrays.toString(temp));//结果为:[9, 5, 17, 3, 8, 17, 4, 6]
		
		Arrays.sort(temp);
		System.out.println(Arrays.toString(temp));//结果为:[3, 4, 5, 6, 8, 9, 17, 17]
		
				
	}
}


方法4、合并后的数组后,使用快排

import java.util.Arrays;


public class ArrayCopy {

	public static void main(String args[]){
		int a[]={9,5,17,3};
		int b[] = {8,17,4,6};
		
		int c[] = copyTest(a,b);
		
		quickSort(c,0,c.length-1);
		System.out.println(Arrays.toString(c));
	}
	
	public static int[] copyTest(int a[],int b[]){
		int temp[] = new int[a.length+b.length];
		System.arraycopy(a, 0, temp, 0, a.length);
		System.arraycopy(b, 0, temp, a.length, b.length);
		System.out.println(Arrays.toString(temp));//结果为:[9, 5, 17, 3, 8, 17, 4, 6]
		return temp;
	}
	
	public static void writeToFile(){
		
	}
	
	//快排
	/*public static void quickSort(int data[],int low,int high){
		int i=low,j=high;
		int key = data[low];
		while(low < high){
			while((low < high) && (data[high]>=key))//不要忘了等于
				high--;
		        data[low] = data[high];
		    while((low <high) && (data[low]<=key))
			   low++;
		        data[high] = data[low];
		}
		
		data[low] = key;
	
	    if(j != high)
			quickSort(data,high+1,j);
	    if(i != low)
			quickSort(data,i,low-1);
	}*/
	
	private static int partition(int data[],int low,int high){
		int key = data[low];
		while(low<high){
			while(low<high && data[high]>=key)
				high--;
				data[low] = data[high];
				
			while(low<high && data[low]<=key)
				low++;
				data[high] = data[low];
		}
		data[low] = key;
		return low;
	}
	
	private static void quickSort(int data[],int low,int high){
		int q;
		if(low<high){
			 q = partition(data,low,high);
		     quickSort(data,q+1,high);
		     quickSort(data,low,q-1);
	}
	}
	}


 备忘:

给定含有1001个元素的数组,其中存放了1-1000之内的整数,只有一个整数是重复的,请找出这个数

分析

求出整个数组的和,再减去1-1000的和

代码

找出出现奇数次的元素

给定一个含有n个元素的整型数组a,其中只有一个元素出现奇数次,找出这个元素。

分析

因为对于任意一个数k,有k ^ k = 0,k ^ 0 = k,所以将a中所有元素进行异或,那么个数为偶数的元素异或后都变成了0,只留下了个数为奇数的那个元素。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值