如何使用扫描仪插入数组列表?

发布于 2024-12-11 05:44:40 字数 3679 浏览 0 评论 0原文

我在尝试弄清楚如何使用扫描仪将整数插入 ArrayList 时遇到一些问题。我在java方面不是那么出色(实际上甚至不是很好),但我只是想弄清楚一些事情,任何帮助都会很棒。

package mySort;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;



public class MergeInsert {
private int limit = 100;
//private int size = 0;
private ArrayList<Integer> ArrayToSort;

public MergeInsert(int x) {
    ArrayToSort = new ArrayList<Integer>(x);
    }

public MergeInsert(Scanner integerScan){
    int j = 0;
    while(integerScan.hasNextInt()){
        this.insert(integerScan.hasNextInt());
        if (j % 10000 == 0){
            long time = System.nanoTime();
            System.out.println(j + "," + time);
        }
    }
}

public void insert(int x){
    for(int i=0; i<ArrayToSort.size(); i++){
        ArrayToSort(size++) = x;
    }
}


//  public MergeInsert(int v){
//      int val = v;
//  }

//    public void insertFile(){
//      try {
//          Scanner integerScan = new Scanner(new FileInputStream(""));
//          while(integerScan.hasNextInt()){
//              new MergeInsert(integerScan.nextInt());
//          }
//      }
//       catch (FileNotFoundException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      }
//    }


public void sort(){

}

public void mergeSort(ArrayList<Integer> in, int low,int high){
    int n = in.size();
    int mid = (high+low)/2;
    if (n<2){  //already sorted
        return;
    }
    if ((high - low) < limit){
        insertionSort(in);
    }
    ArrayList<Integer> in1 = new ArrayList<Integer>(); //helper
    ArrayList<Integer> in2 = new ArrayList<Integer>(); //helper
    int i=0;

    while (i < n/2){ //moves the first half to the helper
        in1.add(in.remove(0));
        i++;
    }
    while (!in.isEmpty()) //moves the second half to the helper
        in2.add(in.remove(0));
    mergeSort(in1, low, mid); //breaks it down some more like mergesort should
    mergeSort(in2, mid+1, high); //does it again
    merge(in1,in2,in); //trying to build it up again
    }

public void merge(ArrayList<Integer> in, ArrayList<Integer> in1, ArrayList<Integer> in2){
    while (!in1.isEmpty() || !in2.isEmpty()) //as long as both helpers still have elements
        if ((in1.get(0).compareTo(in2.get(0)) <= 0)) //comparison to rebuild
            in.add(in1.remove(0)); //building it back up
        else
            in.add(in2.remove(0)); //still building
    while(!in1.isEmpty()) //as long as the first helper isn't empty keep building
        in.add(in1.remove(0));
    while(!in2.isEmpty()) //as long as the second helper isn't empty keep building
        in.add(in2.remove(0));
}

public ArrayList<Integer> insertionSort(ArrayList<Integer> in){
    int index = 1;
    while (index<in.size()){
        insertSorted((int)(in.get(index)),in,index);
        index = index +1;
    }
    return in;
}

public ArrayList<Integer> insertSorted(Integer s, ArrayList<Integer> in, int index){
    int loc = index-1;
    while((loc>=0) || s.compareTo(in.get(loc)) <= 0){
        in.set(loc + 1, in.get(loc));
        loc = loc -1;
    }
    in.set(loc+1, s);
    return in;
    }


/**
 * @param args
 * @throws FileNotFoundException 
 */
public static void main(String[] args) throws FileNotFoundException {
    Scanner integerScan = new Scanner(new FileInputStream("src/myRandomNumbers.txt"));
    MergeInsert myObject = new MergeInsert(integerScan);
    myObject.sort();

}

}

它尚未完全完成,但所有这一切背后的想法是尝试改进合并排序。基本上,一旦元素被分解到某个点,就切入插入排序,因为它通常在非常小的(非常小的相对)数据集上更好。

I'm having some problems trying to figure out how to insert an integer using a scanner into an ArrayList. I'm not that great (actually not even really good) at java but I'm just trying to figure some things out and any help would be great.

package mySort;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;



public class MergeInsert {
private int limit = 100;
//private int size = 0;
private ArrayList<Integer> ArrayToSort;

public MergeInsert(int x) {
    ArrayToSort = new ArrayList<Integer>(x);
    }

public MergeInsert(Scanner integerScan){
    int j = 0;
    while(integerScan.hasNextInt()){
        this.insert(integerScan.hasNextInt());
        if (j % 10000 == 0){
            long time = System.nanoTime();
            System.out.println(j + "," + time);
        }
    }
}

public void insert(int x){
    for(int i=0; i<ArrayToSort.size(); i++){
        ArrayToSort(size++) = x;
    }
}


//  public MergeInsert(int v){
//      int val = v;
//  }

//    public void insertFile(){
//      try {
//          Scanner integerScan = new Scanner(new FileInputStream(""));
//          while(integerScan.hasNextInt()){
//              new MergeInsert(integerScan.nextInt());
//          }
//      }
//       catch (FileNotFoundException e) {
//          // TODO Auto-generated catch block
//          e.printStackTrace();
//      }
//    }


public void sort(){

}

public void mergeSort(ArrayList<Integer> in, int low,int high){
    int n = in.size();
    int mid = (high+low)/2;
    if (n<2){  //already sorted
        return;
    }
    if ((high - low) < limit){
        insertionSort(in);
    }
    ArrayList<Integer> in1 = new ArrayList<Integer>(); //helper
    ArrayList<Integer> in2 = new ArrayList<Integer>(); //helper
    int i=0;

    while (i < n/2){ //moves the first half to the helper
        in1.add(in.remove(0));
        i++;
    }
    while (!in.isEmpty()) //moves the second half to the helper
        in2.add(in.remove(0));
    mergeSort(in1, low, mid); //breaks it down some more like mergesort should
    mergeSort(in2, mid+1, high); //does it again
    merge(in1,in2,in); //trying to build it up again
    }

public void merge(ArrayList<Integer> in, ArrayList<Integer> in1, ArrayList<Integer> in2){
    while (!in1.isEmpty() || !in2.isEmpty()) //as long as both helpers still have elements
        if ((in1.get(0).compareTo(in2.get(0)) <= 0)) //comparison to rebuild
            in.add(in1.remove(0)); //building it back up
        else
            in.add(in2.remove(0)); //still building
    while(!in1.isEmpty()) //as long as the first helper isn't empty keep building
        in.add(in1.remove(0));
    while(!in2.isEmpty()) //as long as the second helper isn't empty keep building
        in.add(in2.remove(0));
}

public ArrayList<Integer> insertionSort(ArrayList<Integer> in){
    int index = 1;
    while (index<in.size()){
        insertSorted((int)(in.get(index)),in,index);
        index = index +1;
    }
    return in;
}

public ArrayList<Integer> insertSorted(Integer s, ArrayList<Integer> in, int index){
    int loc = index-1;
    while((loc>=0) || s.compareTo(in.get(loc)) <= 0){
        in.set(loc + 1, in.get(loc));
        loc = loc -1;
    }
    in.set(loc+1, s);
    return in;
    }


/**
 * @param args
 * @throws FileNotFoundException 
 */
public static void main(String[] args) throws FileNotFoundException {
    Scanner integerScan = new Scanner(new FileInputStream("src/myRandomNumbers.txt"));
    MergeInsert myObject = new MergeInsert(integerScan);
    myObject.sort();

}

}

It's not completely finished but the idea behind all of this is to try and improve on MergeSort. Basically once the elements get broken down to a certain point cut to InsertionSort because it is usually better on really small (really small being relative) sets of data.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦在深巷 2024-12-18 05:44:41
public void insert(int x){
    ArrayToSort.add(x); // add it to the end
}

原因是……即使你去

ArrayToSort = new ArrayList<Integer>(100000);

它的大小仍然是0。它的CAPACITY只是100000。

public void insert(int x){
    ArrayToSort.add(x); // add it to the end
}

The reason is... even if you go

ArrayToSort = new ArrayList<Integer>(100000);

It still has a size of 0. It just has a CAPACITY of 100000.

停滞 2024-12-18 05:44:41

使用添加将对象插入列表中。

另外,按照您现在的代码结构方式,当您尝试调用 add 时,您将收到 NullPointerException ,因为您调用的构造函数永远不会初始化列表。

鉴于您的代码质量,我强烈建议您阅读学习 Java 语言

Use add to insert objects into the list.

Also, the way your code is structured now, you will get a NullPointerException when you attempt to invoke add because the constructor you invoke never initializes the list.

Given the quality of your code, I highly recommend reading Learning the Java Language.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文