您的位置:

java排序函数,java排序函数对负数的排序会反吗

本文目录一览:

java自动排序函数.......

要实现排序功能,一般有两种途径,这里对基本类型不适用,基本类型一般有Arrays中的静态方法.

1.对象本身实现Comparable接口,那么该类的实例就是可以排序的.

有关Comparable:

只要实现了Comparable接口,就可以调用Collections的sort方法对集合中的元素排序.

2.指定一个Comparator,也就是实现了Comparator的类的一个实例.

但是Java本身只提供了一个Comparator的实现,就是Collections.reverseOrder().

该方法返回的是一个已经实现了Comparable接口的反序.

看一下Comparator的全部内容:

public interface Comparator {

int compare(Object o1, Object o2);

boolean equals(Object obj);

}

定义了两个方法,其实我们一般都只需要实现compare方法就行了,因为类都是默认从Object继承

所以会使用Object的equals方法.

Comparator一般都作为一个匿名类出现,对于没有实现Comparable的对象的集合,排序的时候

需要指定一个Comparator.

这里举例说明

对于实现了Comparable的类我们就用最简单的Integer

List list=new ArrayList();

list.add(new Integer(3));

list.add(new Integer(53));

list.add(new Integer(34));

Collections.sort(list);

对于没有实现Comparable的,我们就用Object,按照hashCode大小来排序.

List list= new ArrayList();

list.add(new Object());

list.add(new Object());

list.add(new Object());

Collections.sort(list,new Comparator(){ public int compare(Object o1, Object o2){

return (o1.hashCode()-o2.hashCode());

})

如何在Java中从键盘输入10个数,用选择法按降序排序并输出?

在Java中在数组中保存10个数,调用下面的排序函数,用选择法按降序排序:public static void selectionSort(int[] arr){ for (int i = 0; i arr.length - 1; i++) { int max = i; for (int j = i + 1; j arr.length; j++) { if (arr[max] arr[j]) { max = j; } } if (max != i) { int tmp = arr[max]; arr[max] = arr[i]; arr[i] = tmp; } } }

java定制排序

文件名:Frame1.java

关键代码:

//选择排序法函数

public void select_Sort(int[] num)

{

int i,j,MX;

int temp;

for(i = 0; inum.length-1; i++)

{

MX = i ;

for(j = i+1 ; jnum.length; j++)

{

if(num[MX] num[j])

MX = j ;

}

temp = num[i];

num[i] = num[MX];

num[MX] = temp;

}

}

//插入排序函数

public void insert_Sort(int[] data)

{

int i,j,mx;

int key ;

for(i=1; i data.length -1; i++)

{

key = data[i];

mx = i;

for(j= i-1;j0;j--)

{

if(keydata[j])

{

data[j+1] = data[j];

mx = j ;

}

}

data[mx]=key;

}

}

全部代码

/////////////////////////

package mytest;

import java.awt.BorderLayout;

import java.awt.Dimension;

import javax.swing.JFrame;

import javax.swing.JPanel;

import javax.swing.JLabel;

import java.awt.Rectangle;

import javax.swing.JTextField;

import javax.swing.JButton;

import java.awt.event.ActionEvent;

import java.awt.event.ActionListener;

import java.awt.event.MouseEvent;

import java.awt.event.MouseAdapter;

import java.awt.event.KeyEvent;

import java.awt.event.KeyAdapter;

import javax.swing.JOptionPane;

public class Frame1 extends JFrame {

JPanel contentPane;

JLabel jLabel1 = new JLabel();

JLabel jLabel2 = new JLabel();

JLabel jLabel3 = new JLabel();

JLabel jLabel4 = new JLabel();

JTextField txt1 = new JTextField();

JTextField txtOrder = new JTextField();

JButton btn2 = new JButton();

JTextField txt2 = new JTextField();

JTextField txtIn = new JTextField();

JButton btn1 = new JButton();

public Frame1() {

try {

setDefaultCloseOperation(EXIT_ON_CLOSE);

jbInit();

} catch (Exception exception) {

exception.printStackTrace();

}

}

/**

* Component initialization.

*

* @throws java.lang.Exception

*/

private void jbInit() throws Exception {

contentPane = (JPanel) getContentPane();

contentPane.setLayout(null);

setSize(new Dimension(457, 432));

setTitle("Frame Title");

jLabel1.setText("排序前");

jLabel1.setBounds(new Rectangle(30, 99, 59, 36));

jLabel2.setText("排序后(1)");

jLabel2.setBounds(new Rectangle(27, 183, 66, 36));

jLabel3.setText("排序后(2)");

jLabel3.setBounds(new Rectangle(29, 259, 76, 36));

jLabel4.setText("用户输入");

jLabel4.setBounds(new Rectangle(28, 35, 71, 36));

txt1.setBounds(new Rectangle(116, 182, 307, 33));

txtOrder.setBounds(new Rectangle(118, 106, 307, 37));

btn2.setBounds(new Rectangle(263, 327, 162, 42));

btn2.setText("插入排序");

btn2.addActionListener(new Frame1_btn2_actionAdapter(this));

txt2.setBounds(new Rectangle(118, 259, 307, 33));

txtIn.setText("请输入数字,并按回车键。一次只能输入一个数字");

txtIn.setBounds(new Rectangle(116, 37, 306, 39));

txtIn.addKeyListener(new Frame1_txtIn_keyAdapter(this));

btn1.setBounds(new Rectangle(28, 327, 162, 42));

btn1.setText("选择排序");

btn1.addActionListener(new Frame1_btn1_actionAdapter(this));

contentPane.add(jLabel4);

contentPane.add(jLabel1);

contentPane.add(txtOrder);

contentPane.add(jLabel2);

contentPane.add(txt1);

contentPane.add(jLabel3);

contentPane.add(txt2);

contentPane.add(txtIn);

contentPane.add(btn1);

contentPane.add(btn2);

}

int[] Number1=new int[10];

int[] Number2=new int[10];

int i=0;

public void txtIn_keyPressed(KeyEvent e) {

try{

if(e.getKeyChar()=='\n'){

Number1[i]=Integer.parseInt(txtIn.getText().trim());

Number2[i]=Integer.parseInt(txtIn.getText().trim());

String txt=txtOrder.getText().trim();

txtOrder.setText(txt+txtIn.getText().trim()+",");

txtIn.setText("");

txtIn.requestFocus(true);

if(++i10){

JOptionPane.showMessageDialog(this,"只能输入10个整数!");

txtIn.setText("");

return;

}

}

}catch(Exception ex){

JOptionPane.showMessageDialog(this,ex.getMessage());

txtIn.setText("");

ex.printStackTrace();

}

}

//选择排序法函数

public void select_Sort(int[] num)

{

int i,j,MX;

int temp;

for(i = 0; inum.length-1; i++)

{

MX = i ;

for(j = i+1 ; jnum.length; j++)

{

if(num[MX] num[j])

MX = j ;

}

temp = num[i];

num[i] = num[MX];

num[MX] = temp;

}

}

//选择排序按钮监听方法

public void btn1_actionPerformed(ActionEvent e) {

txt1.setText("");

select_Sort(Number1);//调用选择排序函数

for(int j=0;jNumber1.length;j++){

String txt=txt1.getText().trim();

txt1.setText(txt+Number1[j]+",");

}

}

//插入排序函数

public void insert_Sort(int[] data)

{

int i,j,mx;

int key ;

for(i=1; i data.length -1; i++)

{

key = data[i];

mx = i;

for(j= i-1;j0;j--)

{

if(keydata[j])

{

data[j+1] = data[j];

mx = j ;

}

}

data[mx]=key;

}

}

public void btn2_actionPerformed(ActionEvent e) {

txt2.setText("");

insert_Sort(Number2);

for(int j=0;jNumber2.length;j++){

String txt=txt2.getText().trim();

txt2.setText(txt+Number2[j]+",");

}

}

//主函数

public static void main(String[] arges){

Frame1 F=new Frame1();

F.setVisible(true);

}

}

class Frame1_btn2_actionAdapter implements ActionListener {

private Frame1 adaptee;

Frame1_btn2_actionAdapter(Frame1 adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.btn2_actionPerformed(e);

}

}

class Frame1_btn1_actionAdapter implements ActionListener {

private Frame1 adaptee;

Frame1_btn1_actionAdapter(Frame1 adaptee) {

this.adaptee = adaptee;

}

public void actionPerformed(ActionEvent e) {

adaptee.btn1_actionPerformed(e);

}

}

class Frame1_txtIn_keyAdapter extends KeyAdapter {

private Frame1 adaptee;

Frame1_txtIn_keyAdapter(Frame1 adaptee) {

this.adaptee = adaptee;

}

public void keyPressed(KeyEvent e) {

adaptee.txtIn_keyPressed(e);

}

}

java常用函数方法

Java的一些常用函数,方法总结:

1:数组方面:

数组是一个对象,数组名可以理解和C++里面一样,

1):System.arraycopy(src,srcindex,dst,dstindex,src.length);

2):java.util.Arrays.sort(数组名);//块数排序法进行排序

3):java.util.Arrays.binarySearch(arr,key);//从哪里收,关键值,返回索引.没有的话返回-1

4):java.util.Array.fill(arr,value),设置数组初始值

5):Array.equals(arr1,arr2)//比较两个数组中元素是不是全部相等,是返回true,不是返回false

2:时间方面:

public static String dateToStr(java.util.Date date)

{

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String str = sdf.format(date);

return str;

}

import java.text.SimpleDateFormat;

import java.util.Calendar;

public class DateUtil

{

public static java.util.Date parseDate(String dateStr, String format)

{

java.util.Date date = null;

try

{

java.text.DateFormat df = new java.text.SimpleDateFormat(format);

String dt = dateStr.replaceAll("-", "/");

if ((!dt.equals("")) (dt.length() format.length()))

{

dt += format.substring(dt.length()).replaceAll("[YyMmDdHhSs]", "0");

}

date = (java.util.Date) df.parse(dt);

}

catch (Exception e)

{

}

return date;

}

public static java.util.Date parseDate(String dateStr)

{

return parseDate(dateStr, "yyyy/MM/dd");

}

public static String format(java.util.Date date, String format)

{

String result = "";

try

{

if (date != null)

{

java.text.DateFormat df = new java.text.SimpleDateFormat(format);

result = df.format(date);

}

}

catch (Exception e)

{

}

return result;

}

public static String format(java.util.Date date)

{

return format(date, "yyyy/MM/dd");

}

public static int getYear(java.util.Date date)

{

java.util.Calendar c = java.util.Calendar.getInstance();

c.setTime(date);

return c.get(java.util.Calendar.YEAR);

}

public static int getMonth(java.util.Date date)

{

java.util.Calendar c = java.util.Calendar.getInstance();

c.setTime(date);

return c.get(java.util.Calendar.MONTH) + 1;

}

public static int getDay(java.util.Date date)

{

java.util.Calendar c = java.util.Calendar.getInstance();

c.setTime(date);

return c.get(java.util.Calendar.DAY_OF_MONTH);

}

public static int getHour(java.util.Date date)

{

java.util.Calendar c = java.util.Calendar.getInstance();

c.setTime(date);

return c.get(java.util.Calendar.HOUR_OF_DAY);

}

public static int getMinute(java.util.Date date)

{

java.util.Calendar c = java.util.Calendar.getInstance();

c.setTime(date);

return c.get(java.util.Calendar.MINUTE);

}

public static int getSecond(java.util.Date date)

{

java.util.Calendar c = java.util.Calendar.getInstance();

c.setTime(date);

return c.get(java.util.Calendar.SECOND);

}

public static long getMillis(java.util.Date date)

{

java.util.Calendar c = java.util.Calendar.getInstance();

c.setTime(date);

return c.getTimeInMillis();

}

public static String getDate(java.util.Date date)

{

return format(date, "yyyy/MM/dd");

}

public static String getTime(java.util.Date date)

{

return format(date, "HH:mm:ss");

}

public static String getDateTime(java.util.Date date)

{

return format(date, "yyyy/MM/dd HH:mm:ss");

}

public static java.util.Date addDate(java.util.Date date, int day)

{

java.util.Calendar c = java.util.Calendar.getInstance();

c.setTimeInMillis(getMillis(date) + ((long) day) * 24 * 3600 * 1000);

return c.getTime();

}

public static int diffDate(java.util.Date date, java.util.Date date1)

{

return (int) ((getMillis(date) - getMillis(date1)) / (24 * 3600 * 1000));

}

public static String getMonthBegin(String strdate)

{

java.util.Date date = parseDate(strdate);

return format(date, "yyyy-MM") + "-01";

}

public static String getMonthEnd(String strdate)

{

java.util.Date date = parseDate(getMonthBegin(strdate));

Calendar calendar = Calendar.getInstance();

calendar.setTime(date);

calendar.add(Calendar.MONTH, 1);

calendar.add(Calendar.DAY_OF_YEAR, -1);

return formatDate(calendar.getTime());

}

public static String formatDate(java.util.Date date)

{

return formatDateByFormat(date, "yyyy-MM-dd");

}

public static String formatDateByFormat(java.util.Date date, String format)

{

String result = "";

if (date != null)

{

try

{

SimpleDateFormat sdf = new SimpleDateFormat(format);

result = sdf.format(date);

}

catch (Exception ex)

{

ex.printStackTrace();

}

}

return result;

}

}

Java日期格式化及其使用例子

1 SimpleDateFormat担当重任,怎样格式化都行

import java.util.Date;

import java.text.SimpleDateFormat;

public class Demo

{

public static void main(String[] args)

{

Date now=new Date();

SimpleDateFormat f=newSimpleDateFormat("今天是"+"yyyy年MM月dd日 E kk点mm分");

System.out.println(f.format(now));

f=new SimpleDateFormat("a hh点mm分ss秒");

System.out.println(f.format(now));

}

}

2 从字符串到日期类型的转换:

import java.util.Date;

import java.text.SimpleDateFormat;

import java.util.GregorianCalendar;

import java.text.*;

publicclass Demo

{

public static void main(String[] args)

{

String strDate="2005年04月22日";

//注意:SimpleDateFormat构造函数的样式与strDate的样式必须相符

SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy年MM月dd日");

//必须捕获异常

try

{

Date date=simpleDateFormat.parse(strDate);

System.out.println(date);

}

catch(ParseException px)

{

px.printStackTrace();

}

}

}

3 将毫秒数换转成日期类型

import java.util.Date;

import java.text.SimpleDateFormat;

import java.util.GregorianCalendar;

import java.text.*;

public class Demo

{

public static void main(String[] args)

{

long now=System.currentTimeMillis();

System.out.println("毫秒数:"+now);

Date dNow=new Date(now);

System.out.println("日期类型:"+dNow);

}

}

4 获取系统时期和时间,转换成SQL格式后更新到数据库

java.util.Date d=new java.util.Date(); //获取当前系统的时间

//格式化日期

new java.text.SimpleDateFormat s= new java.text.SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

String dateStr = s.format(d); //转为字符串

使用RS更新数据库,仍然要用rs.updateString,而不是rs.updateDade。

rs.updateString("regtime",dateStr); //regtime字段为datetime类型的

5 按本地时区输出当前日期

Date myDate = new Date();

System.out.println(myDate.toLocaleString());

输出结果为:

2003-5-30

6 如何格式化小数

DecimalFormat df = new DecimalFormat(",###.00");

double aNumber = 33665448856.6568975;

String result = df.format(aNumber);

Sytem. out.println(result);

输出结果为:

33,665,448,856.66

其他:获取毫秒时间 System.currentTimeMillis();

7 在数据库里的日期只以年-月-日的方式输出

定义日期格式:SimpleDateFormat sdf = new SimpleDateFormat(yy-MM-dd);

sql语句为:String sqlStr = "select bookDate from roomBook where bookDate between '2007-4-10' and '2007-4-25'";

输出:

System.out.println(df.format(rs.getDate("bookDate")));