-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayClass.java
More file actions
70 lines (58 loc) · 2.04 KB
/
ArrayClass.java
File metadata and controls
70 lines (58 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import java.util.*;
import java.io.*;
import java.math.*;
public class ArrayClass{
protected int[] input_array;
protected int nElems;
// multiple constructor
public ArrayClass(){
// nothing here
this(0);
}
public ArrayClass(int max){
input_array = new int [max];
nElems = 0;
}
public void insert(int value){
input_array[nElems++] = value;
}
public void display(int stop_index){
for(int j = 0; j < nElems; j++){
System.out.print(input_array[j] + " ");
if(stop_index >= 0 && j == stop_index){
break;
}
}
System.out.println("");
}
public void swap(int i, int j){
int temp = input_array[i];
input_array[i] = input_array[j];
input_array[j] = temp;
}
public void read_file_and_populate(String file_loc) throws IOException{
// Read input file
FileInputStream fil = new FileInputStream(file_loc);
BufferedReader br = new BufferedReader( new InputStreamReader(fil));
String element = null;
while( ( element = br.readLine()) != null){
insert(Integer.parseInt(element));
}
}
public int file_line_count(String filepath) throws IOException{
// Execute terminal command to count line of file
String[] Command = new String[]{"/usr/bin/wc", "-l", filepath};
ProcessBuilder builder = new ProcessBuilder(Command);
// Redirect errorstream
builder.redirectErrorStream(true);
Process process = builder.start();
// Read output on Terminal as our input stream
InputStream is = process.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
// Get line count for filepath
String line = reader.readLine();
String[] line_parts = line.split(" ");
// Read Input file and initialize the input array with size = line count
return Integer.parseInt(line_parts[line_parts.length - 2]);
}
}