所以我必须使用String.split和Bubblesort对东西进行排序,问题是:我从来没有告诉过如何使用String.split。 此外,我们需要将其从文件中删除。
public class A2017125SortingThings {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws FileNotFoundException {
int x;
int y;
int count;
String Name [];
String Lastname [];
String Zip [];
String State[];
String City[];
File xyz = new File ("A20171204AddressSortList.dat");
Scanner infile = new Scanner(xyz);
count = infile.nextInt();
Name = new String [count+1];
Lastname = new String [count+1];
Zip = new String [count+1];
State = new String [count+1];
City = new String [count+1];
String.split("newdata");
Name = newdata [0];
Lastname = newdata [1];
Zip = newdata [2];
State = newdata [3];
City = newdata [4];
这是我老师给我们的一个例子,我正在尝试使用它(我编辑并添加了我需要编辑的内容,这是String.split的一切)但当然它不起作用,“非静态方法split(java.lang.String)不能从静态上下文引用“是错误,如果我无法弄清楚如何从bubblesort开始?
答案 0 :(得分:1)
您收到的错误告诉您,您正在使用的String split
函数是非静态的。在类上调用静态方法,一个很好的例子是Math.random()
。在对象上调用非静态方法,例如,如果您有一个String,则可以在其上调用split
。
String str = "this_is_a_bunch_of_words_separated_by_underscores";
String[] words = str.split("_");
如果您阅读String.split documentation,您可以看到此方法的作用是将正则表达式作为参数,(这里我只使用了“_”字符,以匹配下划线)和返回一个字符串数组,由给定的表达式(“_”)分隔。因此String[] words
将是一个包含每个单词的数组,例如[this, is, a, bunch, of, words, separated, by, underscores]
答案 1 :(得分:-1)
使用Collections.sort(your array)