繁体   English   中英

如何使用扫描仪插入arraylist?

[英]how to insert into arraylist using a scanner?

我在尝试弄清楚如何使用扫描仪将整数插入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();

}

}

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

使用add将对象插入列表。

此外,现在构建代码的方式,当您尝试调用add时,您将获得NullPointerException ,因为您调用的构造函数从不初始化列表。

考虑到代码的质量,我强烈建议您阅读学习Java语言

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

原因是......即使你去了

ArrayToSort = new ArrayList<Integer>(100000);

它的大小仍为0.它的容量为100000。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM