-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathALint.java
More file actions
73 lines (68 loc) · 1.56 KB
/
ALint.java
File metadata and controls
73 lines (68 loc) · 1.56 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
71
72
73
//cbasurto: hw 4 problem 1 making an array list from an array
import java.util.Arrays;
public class ALint
{
//field
private int [] arr;
//constructors
public ALint()
{
arr = new int [0];
}
//method to add an item to an array
public void add(int item)
{ //Arrays.copyOf replicates the first array
//int [] newArray = new arr [1];
arr = Arrays.copyOf(arr, arr.length + 1);
arr[arr.length-1] = item;
}
//void add(int index,int item)
public void add(int index, int item)
{
arr = Arrays.copyOf(arr, arr.length + 1);
int i = 0;
for (i= arr.length-1; i>index; i--)
{
arr[i] = arr[i-1]; //shifting everything to the right by 1 position
}
arr[index] = item; //inserting the item at index
}
//int get(int index)
public int get(int index)
{
return arr[index]; //access value at index and return
}
//void set(int index, int item)
public void set(int index, int item)
{
arr[index] = item; //arr[index] to access value at given index in array
}
//int remove(int index) (return the value you removed)
public int remove(int index)
{
int ret = arr[index];
int i = index;
for (i=index; i<=arr.length-2; i++)
{
arr[i]= arr[i+1];
}
arr = Arrays.copyOf(arr, arr.length -1);
return ret;
}
//int size()
public int size()
{
return arr.length;
}
//String toString()
public String toString()
{
String outStr = "";
int i = 0;
for (i=0; i<arr.length; i++)
{
outStr = outStr + arr[i] + " ";
}
return outStr;
}
}