Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Java中不使用数组对句子进行排序_Java_Loops_While Loop - Fatal编程技术网

在Java中不使用数组对句子进行排序

在Java中不使用数组对句子进行排序,java,loops,while-loop,Java,Loops,While Loop,我希望能够对句子示例输入进行排序Python:PHP:C++:C:Java:HTML 不使用数组或数组方法对句子排序 输出应该是这样的 Top programming languages in alphabetical order: 1. C 2. C++ 3. HTML 4. Java 5. PHP 6. Python 这是我开始使用的,但我被卡住了,在互联网上找不到任何不使用阵列的东西 import java.util.*; public class SortProgram {

我希望能够对句子示例输入进行排序
Python:PHP:C++:C:Java:HTML

不使用数组或数组方法对句子排序

输出应该是这样的

Top programming languages in alphabetical order:
1. C
2. C++
3. HTML
4. Java
5. PHP
6. Python
这是我开始使用的,但我被卡住了,在互联网上找不到任何不使用阵列的东西

import java.util.*;

public class SortProgram {
    public static void main(String[] args) {
        // Declare Variables
        String topLang = "";
        int separator = 0;
        String holder = "";
        String top = "";

        // Create a Scanner object attached to the keyboard
        Scanner input = new Scanner(System.in);

        // input
        System.out.print("Enter a list of the top programming languages: ");
        topLang = input.next();

        // separate each word
        while (topLang.length() > 0) {
            separator = topLang.indexOf(":");
            holder = topLang.substring(0, separator);
            topLang = topLang.substring(separator, topLang.length());
            
        }

        System.out.println("  Position Language");
        System.out.println("==========================");
    }

}

最简单的方法是使用Java 8流API:

String topLang=“Python:PHP:C++:C:Java:HTML”;
Pattern.compile(“:”).splitAsStream(topLang.sorted().forEach(System.out::println);
输出

C
C++
HTML
JAVA
PHP
python

另一种方法是使用
列表
,与问题中的实现保持距离。如果您根本不需要数组,请使用非数组列表实现,如
LinkedList

String topLang=“Python:PHP:C++:C:Java:HTML”;
列表结果=新建LinkedList();
int start=0;
for(int-end;(end=topLang.indexOf(':',start))!=-1;start=end+1)
添加(topLang.substring(start,end));
添加(topLang.substring(start));
集合。排序(结果);
for(字符串s:结果)
系统输出打印项次;

与上面相同的输出。

最简单的方法是使用Java 8流API:

String topLang=“Python:PHP:C++:C:Java:HTML”;
Pattern.compile(“:”).splitAsStream(topLang.sorted().forEach(System.out::println);
输出

C
C++
HTML
JAVA
PHP
python

另一种方法是使用
列表
,与问题中的实现保持距离。如果您根本不需要数组,请使用非数组列表实现,如
LinkedList

String topLang=“Python:PHP:C++:C:Java:HTML”;
列表结果=新建LinkedList();
int start=0;
for(int-end;(end=topLang.indexOf(':',start))!=-1;start=end+1)
添加(topLang.substring(start,end));
添加(topLang.substring(start));
集合。排序(结果);
for(字符串s:结果)
系统输出打印项次;

与上面相同的输出。

这里是另一个使用PriorityQueue和regex的解决方案

try (Scanner scanner = new Scanner(System.in)) {
    Queue<String> sortedLanguages = new PriorityQueue<>();
    System.out.print("Enter a list of the top programming languages: ");
    String input = scanner.next();
    Matcher matcher = Pattern.compile("[^:]+").matcher(input);
    while (matcher.find()) {
        sortedLanguages.add(matcher.group());
    }
    System.out.print("Top programming languages in alphabetical order:");
    for (int i = 1, size = sortedLanguages.size(); i <= size; i++) {
        System.out.printf("\n%d. %s", i, sortedLanguages.poll());
    }
}
try(扫描器=新扫描器(System.in)){
Queue sortedLanguages=new PriorityQueue();
System.out.print(“输入顶级编程语言的列表:”);
字符串输入=scanner.next();
Matcher Matcher=Pattern.compile(“[^::]+”).Matcher(输入);
while(matcher.find()){
sortedLanguages.add(matcher.group());
}
System.out.print(“按字母顺序排列的顶级编程语言:”);

对于(inti=1,size=sortedLanguages.size();i,这里是另一个使用PriorityQueue和regex的解决方案

try (Scanner scanner = new Scanner(System.in)) {
    Queue<String> sortedLanguages = new PriorityQueue<>();
    System.out.print("Enter a list of the top programming languages: ");
    String input = scanner.next();
    Matcher matcher = Pattern.compile("[^:]+").matcher(input);
    while (matcher.find()) {
        sortedLanguages.add(matcher.group());
    }
    System.out.print("Top programming languages in alphabetical order:");
    for (int i = 1, size = sortedLanguages.size(); i <= size; i++) {
        System.out.printf("\n%d. %s", i, sortedLanguages.poll());
    }
}
try(扫描器=新扫描器(System.in)){
Queue sortedLanguages=new PriorityQueue();
System.out.print(“输入顶级编程语言的列表:”);
字符串输入=scanner.next();
Matcher Matcher=Pattern.compile(“[^::]+”).Matcher(输入);
while(matcher.find()){
sortedLanguages.add(matcher.group());
}
System.out.print(“按字母顺序排列的顶级编程语言:”);

对于(int i=1,size=sortedLanguages.size();i您可以使用
TreeSet
数据结构来存储已排序的字符串:

import java.util.*;
导入java.util.concurrent.AtomicInteger;
公共类排序程序{
//Python:PHP:C++:C:Java:HTML
公共静态void main(字符串[]args){
//声明变量
字符串topLang=“”;
int分隔符=0;
字符串持有者=”;
字符串top=“”;
//创建连接到键盘的扫描仪对象
扫描仪输入=新扫描仪(System.in);
//输入
System.out.print(“输入顶级编程语言的列表:”);
topLang=input.next();
//分词
设置语言=新树集();
while(topLang.length()>0){
分隔符=topLang.indexOf(“:”);
如果(分隔符==-1){
分隔符=topLang.length()-1;
}
holder=topLang.子字符串(0,分隔符);
topLang=topLang.substring(分隔符+1);
语言。添加(持有人);
}
AtomicInteger位置=新的AtomicInteger(1);
System.out.println(“位置语言”);
System.out.println(“==============================================”);
languages.forEach(language->System.out.println(position.getAndIncrement()+”:“+language));
}
}

o如果您根本不想使用任何数据结构,您可以通过使用字符串构建一个类似于气泡排序的排序算法。

您可以使用
树集
数据结构来存储排序后的字符串:

import java.util.*;
导入java.util.concurrent.AtomicInteger;
公共类排序程序{
//Python:PHP:C++:C:Java:HTML
公共静态void main(字符串[]args){
//声明变量
字符串topLang=“”;
int分隔符=0;
字符串持有者=”;
字符串top=“”;
//创建连接到键盘的扫描仪对象
扫描仪输入=新扫描仪(System.in);
//输入
System.out.print(“输入顶级编程语言的列表:”);
topLang=input.next();
//分词
设置语言=新树集();
while(topLang.length()>0){
分隔符=topLang.indexOf(“:”);
如果(分隔符==-1){
分隔符=topLang.length()-1;
}
holder=topLang.子字符串(0,分隔符);
topLang=topLang.substring(分隔符+1);
语言。添加(持有人);
}
AtomicInteger位置=新的AtomicInteger(1);
System.out.println(“位置语言”);
System.out.println(“==============================================”);
languages.forEach(language->System.out.println(position.getAndIncrement()+”:“+language));
}
}
o如果你不知道的话