how to insert into arraylist using a scanner?
-
10-02-2021 - |
Question
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.
Solution
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.
OTHER TIPS
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.