我有一个带有属性字符串和数字的输入 我必须对与数字对应的字符串进行排序,如果两个字符串具有相同的对应数字,则字符串应按字典顺序排序。
例如输入
abc 2 嗨3 再见3
输出
再见3 嗨3 abc 2如何在java中实现这一点。我应该选择哪种数据结构来解决这个问题?
答案 0 :(得分:1)
您可以尝试这种方法
import java.io.*;
import java.util.*;
// required data structure
class A
{
String s;
int num;
}
class B
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
String input=sc.nextLine(); //reading input
StringTokenizer st=new StringTokenizer(input);
Vector<A> v=new Vector<A>(); //for storing objects of class A
while(st.hasMoreTokens())
{
A a=new A(); //creating objects
a.s=st.nextToken();
a.num=Integer.parseInt(st.nextToken());
v.add(a);
}
//sort
for(int i=0;i<v.size();i++)
{
for(int j=0;j<v.size()-1;j++)
{
if(v.get(j).num<v.get(j+1).num)
{
A temp=v.get(j);
v.set(j,v.get(j+1));
v.set(j+1,temp);
}
else
if(v.get(j).num==v.get(j+1).num)
{
if(v.get(j).s.compareTo(v.get(j+1).s)>0)
{
A temp=v.get(j);
v.set(j,v.get(j+1));
v.set(j+1,temp);
}
}
}
} //sort end
for(int i=0;i<v.size();i++)
System.out.print(v.get(i).s+" "+v.get(i).num+" ");
}
}
答案 1 :(得分:1)
或者,您可以使用优先级队列并拥有一个存储类 覆盖CompareTo。像这样:
/**
* A container for data tuples, eg in "abc 2 hi 3 bye 3 q 2 a 1"
* (abc 2) would be one DataValue Object
*
* make it implement comparable and override compare to so that the objects with higher num
* values are considered a "higher priority"
* @author Dragos
*
*/
public class DataValue implements Comparable<DataValue> {
private String str;
private Integer num;
public DataValue(String str, Integer num) {
this.str = str;
this.num = num;
}
@Override
public int compareTo(DataValue o) {
if (o.num > this.num)
return 1;
else
return -1;
}
@Override
public String toString() {
return str + " " + num;
}
// GETTERS:
public String getStr() {
return str;
}
public Integer getNum() {
return num;
}
}
然后是主要课程:
import java.util.*;
public class SortingStrings {
private static PriorityQueue <DataValue> pq = new PriorityQueue<DataValue>();
public static void main (String [] args) {
String inputString = "abc 2 hi 3 bye 3 q 2 a 1";
// split the input into an array:
String [] asArray = inputString.split(" ");
// now load all the string and integer values into their own array lists:
List<String> allSubStrings = new ArrayList<String> ();
List<Integer> allSubNumbers = new ArrayList<Integer> ();
for (int i = 0; i< asArray.length; i++) {
// for even positions:
if (i%2 == 0)
// we parse a string
allSubStrings.add(asArray[i]);
// for odd positions
else
// we parse a number, and are ready to create a DataValue Object:
allSubNumbers.add(Integer.parseInt(asArray[i]));
}
// now go through both array lists creating DataValue objects and adding them to the PriorityQueue:
for (int i = 0; i < allSubStrings.size(); i++) {
pq.add(new DataValue(allSubStrings.get(i),allSubNumbers.get(i)));
}
// now pop from the priority queue to get the resulting string:
for (int i = 0; i < allSubNumbers.size(); i ++) {
System.out.print(pq.remove() + " ");
}
}
}
答案 2 :(得分:1)
import java.io.Console;
import java.util.Scanner;
public class s1 {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String ss=sc.nextLine();
String[] a;
String temp;
a = ss.split(" ");
//sort
for (int i = 1; i < a.length; i += 2) {
for (int j = i + 2; j < a.length; j += 2) {
if (Integer.parseInt(a[i]) < Integer.parseInt(a[j])) {
//swapping if numbers were different
temp = a[i];
a[i] = a[j];
a[j] = temp;
temp = a[i - 1];
a[i - 1] = a[j - 1];
a[j - 1] = temp;
} else if (Integer.parseInt(a[i]) == Integer.parseInt(a[j])) {
if (a[i - 1].compareTo(a[j - 1]) > 0) {
//swapping if numbers were same, then checking alphabets
temp = a[i];
a[i] = a[j];
a[j] = temp;
temp = a[i - 1];
a[i - 1] = a[j - 1];
a[j - 1] = temp;
}
}
}
}
for (int i = 0; i < a.length; i++)
System.out.print(a[i]+" ");
}
}