Java中的多态排序转换
我需要帮助解决以下问题。首先我列出了Numbers.java,下面是我对Strings.java的尝试。请帮忙Java中的多态排序转换,java,arrays,sorting,Java,Arrays,Sorting,我需要帮助解决以下问题。首先我列出了Numbers.java,下面是我对Strings.java的尝试。请帮忙 编写一个程序Strings.java,类似于Numbers.java,它读入字符串对象数组并对它们进行排序。您可以只复制和编辑Numbers.java package hw05; /* Demonstrates selectionSort on an array of integers. */ import java.util.Scanner; public class
package hw05;
/*
Demonstrates selectionSort on an array of integers.
*/
import java.util.Scanner;
public class Numbers {
// --------------------------------------------
// Reads in an array of integers, sorts them,
// then prints them in sorted order.
// --------------------------------------------
public static void main(String[] args) {
Integer[] intList;
int size;
Scanner scan = new Scanner(System.in);
System.out.print("\nHow many integers do you want to sort? ");
size = scan.nextInt();
intList = new Integer[size];
System.out.println("\nEnter the numbers...");
for (int i = 0; i < size; i++)
intList[i] = scan.nextInt();
Sorting.selectionSort(intList);
System.out.println("\nYour numbers in sorted order...");
for (int i = 0; i < size; i++)
System.out.print(intList[i] + " ");
System.out.println();
}
}
包装hw05;
/*
演示整数数组上的selectionSort。
*/
导入java.util.Scanner;
公共类编号{
// --------------------------------------------
//读入整数数组,对其进行排序,
//然后按顺序打印。
// --------------------------------------------
公共静态void main(字符串[]args){
整数[]整数列表;
整数大小;
扫描仪扫描=新扫描仪(System.in);
System.out.print(“\n要排序多少个整数?”);
size=scan.nextInt();
intList=新整数[大小];
System.out.println(“\n输入数字…”);
对于(int i=0;i
package hw05;
/*
Demonstrates selectionSort on an array of strings.
*/
import java.util.Scanner;
public class Strings {
// --------------------------------------------
// Reads in an array of strings, sorts them,
// then prints them in sorted order.
// --------------------------------------------
public static void main(String[] args) {
String[] stringList;
String size;
Scanner scan = new Scanner(System.in);
System.out.print("\nHow many strings do you want to sort? ");
size = scan.nextLine();
stringList = new String[size];
System.out.println("\nEnter the strings...");
for (String i = 0; i < size; i++)
stringList[i] = scan.nextLine();
Sorting.selectionSort(stringList);
System.out.println("\nYour strings in sorted order...");
for (String i = 0; i < size; i++)
System.out.print(stringList[i] + " ");
System.out.println();
}
}
包装hw05;
/*
演示字符串数组上的selectionSort。
*/
导入java.util.Scanner;
公共类字符串{
// --------------------------------------------
//读入字符串数组,对其进行排序,
//然后按顺序打印。
// --------------------------------------------
公共静态void main(字符串[]args){
字符串[]字符串列表;
字符串大小;
扫描仪扫描=新扫描仪(System.in);
System.out.print(“\n要排序多少字符串?”);
size=scan.nextLine();
stringList=新字符串[大小];
System.out.println(“\n输入字符串…”);
用于(字符串i=0;i
由于此处的声明,您将得到一个错误:
String size;
当您稍后将其用作整数时:
stringList = new String[size];
您应该使用Integer.parse,如下所示:
stringList = new String[Integer.parseInt(size)];
但是您稍后将再次使用size来进行循环,因此我建议将其恢复为Numbers.java中的int。请确保将扫描仪更改回:
scan.nextInt();
除此之外,我看不出还有其他问题。如果您发布了输出并指出什么不起作用?到底是什么问题?我需要转换numbers.java程序以读入字符串对象数组并对其排序。>您可以复制和编辑numbers.java。我认为除非您解释这段代码所面临的问题,否则不会发生这种情况。public static void main(String[]args){Integer[]intList;int size;Scanner scan=new Scanner(System.in);System.out.print(“\n要排序多少字符串?”);size=scan.nextInt();stringList=new String[Integer.parseInt(size)];System.out.println(\n输入字符串…”);for(int i=0;i
stringList=new string[Integer.parseInt(size)];
行解析字符串中的int;您需要将其更改回stringList=new string[size];
以后,请解释您遇到的错误,以便更容易找到问题。