JAVA中如何用array类中的sort排序方法实现数组排序

2024年11月29日 02:52
有3个网友回答
网友(1):

public static void main(String[] args) {
int[] iii={4,5,61,3,2,4,26,21,2,-82,34};
Arrays.sort(iii);
for (int i : iii) {
System.out.println(i);
}
}

网友(2):

public class testjava {
public static void main (String args[]) {
int src[] = {4, 5, 2, 88, 3, 99, 88, 55};
java.util.Arrays.sort(src);
int i=src.length;
System.out.println("第二大的数是:" + src[i-3]);
for(i=0;i System.out.print(src[i]+"\t");
}
}
}

网友(3):

package ch03;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class JavaBean {

//输出属性
public static void variable(PrintWriter pw,String value )
{
pw.println(" private "+value+";");
}

//输出有参构造器代码块
public static void parameter(PrintWriter pw,String value )
{
pw.println(" this."+value+"="+value+";");
}

//每个属性创建Getters和Setters方法
public static void middle(PrintWriter pw,String middle1,String middle2)
{
//将首字母转换大写转换
char c=middle2.charAt(0);
char d=Character.toUpperCase(c);
String caps = middle2.replace(c, d);

pw.println(" public "+middle1+" get"+caps+"(){"+"\r\n"
+" return "+middle2+";"+"\r\n"
+" }"
+"\r\n"
+" public void"+" set"+caps+"(){"+"\r\n"
+" }"
+"\r\n");
}

//读出文件内的行数
public static int length()
{
int result=0;
FileReader f1=null;
BufferedReader br=null;
try {
f1=new FileReader("D:/class.txt");
br=new BufferedReader(f1);

while(br.readLine()!=null)
{
result++;
}
} catch (Exception e) {
e.printStackTrace();
}finally{
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return result;
}

public static void main(String[] args) {

//读取文件内容
FileReader f1=null;
BufferedReader br=null;
String [] a=new String [length()];
try {
f1=new FileReader("D:/class.txt");
br=new BufferedReader(f1);

for(int i=0;i<=length()-1;i++)
{
a[i]=br.readLine();
}
} catch (Exception e) {
e.printStackTrace();
}

File f=new File("D:/"+a[0]+".txt");
FileWriter f2=null;
PrintWriter pw=null;

//将属性拆分成属性类型和属性名称,并存入新数组c中
List list = new ArrayList();
for(int j=1;j {
String [] b=new String[2];
b=a[j].split(" ");
list.add(b[0]);
list.add(b[1]);
}
String [] c= (String[]) list.toArray(new String[1]);

try {
f2=new FileWriter(f);
pw=new PrintWriter(f2);
pw.println("public class "+a[0]+"{"+"\r\n");//定义类名
for(int k=1;k {
variable(pw,a[k]);
}
pw.print( "\r\n");

//定义有参构造器
pw.print( " public "+a[0]+"(");
for(int h=1;h {
pw.print(a[h]);

if(h==a.length-1)
{
break;
}
pw.print(",");
}
pw.print(")"+"{"+"\r\n");
for(int g=1;g<=c.length;g+=2)
{
parameter(pw,c[g]);
}
pw.println(" }");

//定义无参构造器
pw.println( " public "+a[0]+"("+")"+"{"+"\r\n"+" }"+"\r\n");

//输出每个属性创建Getters和Setters方法
for(int y=0;y<=c.length-1;y+=2)
{
middle(pw,c[y],c[y+1]);
}

pw.println("}");

} catch (Exception e) {
e.printStackTrace();
}finally
{
pw.close();
}
}
}