Home  >  Article  >  Java  >  How to implement the function of pointers in java

How to implement the function of pointers in java

(*-*)浩
(*-*)浩Original
2019-05-28 09:16:454639browse

First of all, let’s take a look at what a pointer is in C language. Literally, it is a structure similar to the positioning function. The most important function of a pointer is to implement a callback function. The so-called callback function means that the function is registered somewhere first, and it will be called when needed later. Callback functions are generally used to intercept messages, obtain system information or handle asynchronous events.

How to implement the function of pointers in java

How to implement functions similar to function pointers in C language

In Java language There is no concept of pointers. You can use interfaces and classes to achieve the same effect. You should first define an interface, then declare the method to be called in the interface, and then implement this interface (different functional implementations, such as an ascending order and a descending order). ), and finally an object of this implementation class is given as a parameter to the calling program, and the calling program calls the specified function through this parameter.

The specific implementation is as follows:

interface IntCompare
{
	public int cmp(int a,int b);
}

class Cmp1 implements IntCompare
{
	public int cmp(int a,int b)
	{
		if(a>b)
			return 1;
		else if(a<b)
			return -1;
		else 
			return 0;
	}
}

class Cmp2 implements IntCompare
{
	public int cmp(int a,int b)
	{
		if(a>b)
			return -1;
		else if(a<b)
			return 1;
		else
			return 0;
	}
}

class HelloWorld
{
	public static void main(String[] args)
	{
		int[] array1 = {7,3,19,40,4,7,1};
		insertSort(array1,new Cmp1());
		System.out.println("升序排列");
		for(int i=0;i<array1.length;i++)
		{
			System.out.print(array1[i]+" ");
		}
	
		System.out.println();
		int[] array2 = {7,3,19,40,4,7,1};
		insertSort(array2,new Cmp2());
		System.out.println("降序排列");
		for(int i =0;i<array2.length;i++)
		{
			System.out.print(array2[i]+" ");
		}
		
	}
									
	public static void insertSort(int[] a,IntCompare cmp)	
	{
		if(a!=null)
		{
			
			for(int i=1;i<a.length;i++)
			{
				int temp = a[i],j=i;
				if(cmp.cmp(a[j-1], temp)==1)    
				{
					while(j>=1&&cmp.cmp(a[j-1],temp)==1)	
					{
						a[j] = a[j-1];
						j--;
					}
				}
				a[j] = temp;
			}
			for(int i=1;i<a.length;i++)
			{
				int temp = a[i];
				int j = i-1;
				while(j>=0&&cmp.cmp(a[j], temp)==1)
				{
					a[j+1] = a[j];
					j--;
				}
				a[j+1] = temp;
			}
		}
	}
}

The above is the detailed content of How to implement the function of pointers in java. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn