您的位置:首页 > 其它

对数字段进行排序,String,Integer,Arrays,ArrayList,Scanner

2017-12-14 08:50 260 查看
package test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class LianXi {

public static void main(String[] args) {
System.out.println("Enter the number, enter 's' to exit:");
ArrayList<Integer> list = new ArrayList<>();
String s = "";
do {
Scanner scanner = new Scanner(System.in);
s = scanner.nextLine();
if (s.equals("s")) {
break;
} else {
list.add(Integer.parseInt(s));
}
} while (!s.equals("s"));
int[] ints = new int[list.size()];
for (int i = 0; i < list.size(); i++) {
ints[i] = list.get(i);
}
Arrays.sort(ints);
for (int i : ints) {
System.out.println(i + "");

}
}
}
/*
String
boolean     equals(Object anObject)     // ""
Compares this string to the specified object.

Integer
static int  parseInt(String s)
Parses the string argument as a signed decimal integer.
static int  parseInt(String s, int radix)
Parses the string argument as a signed integer in the radix specified by the second argument.

Arrays
static void     sort(byte[] a)
Sorts the specified array into ascending numerical order.
static void     sort(byte[] a, int fromIndex, int toIndex)
Sorts the specified range of the array into ascending order.

ArrayList<object>
boolean     add(E e)
Appends the specified element to the end of this list.
E   get(int index)
Returns the element at the specified position in this list.
remove()
set()
size()
contains()

Scanner
Scanner(InputStream source)
Constructs a new Scanner that produces values scanned from the specified input stream.
String  nextLine()
Advances this scanner past the current line and returns the input that was skipped.
*/
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: