In: Computer Science
MyArrayForString .java
public class MyArrayForString {
String[] strings;
int stringElements;
public MyArrayForString() { // Constructor. automatically called
when creating an instance
stringElements = 0;
strings = new String[5];
}
public MyArrayForString(int capacity) { // Constructor.
automatically called when creating an instance
stringElements = 0;
strings = new String[capacity];
}
public MyArrayForString(String[] strings1) {
strings = new String[strings1.length];
for(int i=0;i<strings1.length;i++)
strings[i] = strings1[i];
stringElements = strings1.length;
}
void printArray(){ // cost, times
System.out.printf("printArray(%d,%d):
",stringElements,strings.length);
for(int i=0; i<stringElements;i++)
System.out.print(strings[i]+" ");
System.out.println();
}
int linearSearch(String val) {
for(int i=0;i<stringElements;i++)
if(strings[i] == val)
return i;
return -1;
}
int binarySearch(String val) {
int start = 0;
int end = strings.length - 1;
int mid;
while(start <= end) {
mid = (start + end)/2;
if(val == strings[mid])
return mid;
else if(val.compareTo(strings[mid])>0)
end = mid-1;
else // val > strings[mid]
start = mid + 1;
}
return -1;
}
private void enlarge() {
// String up the size of strings;
String[] new_strings = new String[strings.length*2];
for(int i=0;i<stringElements;i++)
new_strings[i] = strings[i];
strings = new_strings;
}
void add(String val) {
if(isFull()) // if(stringElements == strings.length)
enlarge();
strings[stringElements] = val;
stringElements++;
}
public void addOrder(int idx, String[] valArray) {
// add all elements of valArray from the specified position of
strings.
// need to keep order
// eg) [10,20,30] --> addOrder(1,{1,2}) makes
{10,1,2,20,30}
ensureCapacity(stringElements + valArray.length);
// Here we can safely assume 'strings' has at least 'stringElements
+ valArray.length' spaces
for(int i=stringElements-1; i>=idx ; i--)
strings[i+valArray.length] = strings[i];
for(int i=0;i<valArray.length;i++)
strings[i+idx] = valArray[i];
stringElements += valArray.length;
}
private void ensureCapacity(int count) {
if(count <= strings.length)
return;
// need more space
String[] new_strings = new String[count];
for(int i=0;i<stringElements;i++)
new_strings[i] = strings[i];
strings = new_strings;
}
int remove(String val){
// search for an element that is equal to val. and remove it from
'strings'
int idx = linearSearch(val);
if(idx < 0)
return 0;
// fill the location with the last element
strings[idx] = strings[stringElements-1];
stringElements--;
return 1;
}
// void removeAll(int val) { // worst-case: latter half elements
are equal to val
// // remove(10) in strings = {1, 2, 3, 2, 4, ..., 10, 10, 10,
10}
// // O(N) * N = O(N*N)
// // search for all the elements equal to val and remove
them.
// while(remove(val) > 0);
// }
void removeAll(String val) { // O(N)
int j = 0;
for(int i=0;i<stringElements;i++)
if(strings[i] != val)
strings[j++] = strings[i];
stringElements = j;
}
String findMin() {
// return the minimum value among elements in strings;
String minV = strings[0];
for(int i=1;i<stringElements;i++)
if( strings[i].compareTo(minV) > 0)
minV = strings[i];
return minV;
}
void sort() { // There is a bug in this code. Find it.
// sort 'strings' in ascending order. eg) 10, 20 , 30
for(int i=0;i<stringElements-1;i++) {
int minIdx = i;
for(int j=i+1;j<stringElements;j++)
if( strings[j].compareTo(strings[minIdx]) > 0)
minIdx = j;
// swap between strings[i] and strings[minIdx]
String temp = strings[i];
strings[i] = strings[minIdx];
strings[minIdx] = temp;
}
}
String[] toArray() {
// return a copy of the array only with valid elements
//return strings; // this is not a right to return a copy.
String[] new_strings = new String[stringElements];
for(int i=0;i<stringElements;i++)
new_strings[i] = strings[i];
return new_strings;
}
public MyArrayForString clone(){
// return a copy of this instance/object
MyArrayForString strings1 = new
MyArrayForString(this.toArray());
return strings1;
}
public void clear() {
stringElements = 0;
}
// In the above class ‘MyArray’, define a new method
‘getElements(int start, int end)’
// that returns a new array. The new array should have
elements
// of ‘strings’ from index ‘start’ to ‘end’ inclusively.
// For example, suppose strings = {10,20,30,40,50}. Then
getElements(2,4)
// returns a new array {30,40,50}. Assume that index ‘start’ and
‘end’ are valid
// (you don’t need to check their validity).
public String[] getElements(int start, int end) {
String[] new_strings = new String[end-start+1];
for(int i= start; i <= end ; i++)
new_strings[i-start] = strings[i];
return new_strings;
}
boolean isEmpty() { return stringElements == 0; }
boolean isFull() { return stringElements==strings.length; }
}
MyArrayDemo.java
public class MyArrayDemo {
static void printArray(String[] strings){
for(int i=0; i<strings.length;i++)
System.out.print(strings[i]+" ");
System.out.println();
}
public static void main(String[] args) {
MyArrayForString mystrings1 = new MyArrayForString();
mystrings1.add("Kevin");
mystrings1.add("Bob");
mystrings1.add("John");
mystrings1.add("David");
mystrings1.printArray();
System.out.println(mystrings1.linearSearch("John"));
System.out.println(mystrings1.linearSearch("Mic"));
mystrings1.remove("Bob");
mystrings1.printArray();
String s[] = {"David","Peter", "David"};
mystrings1.addOrder(1,s);
mystrings1.printArray();
mystrings1.removeAll("David");
mystrings1.printArray();
}
}
output screenshot:
MyArrayForChar.java
public class MyArrayForChar {
char[] chars;
int charElements;
public MyArrayForChar() { // Constructor. automatically called when
creating an instance
charElements = 0;
chars = new char[5];
}
public MyArrayForChar(int capacity) { // Constructor. automatically
called when creating an instance
charElements = 0;
chars = new char[capacity];
}
public MyArrayForChar(char[] chars1) {
chars = new char[chars1.length];
for(int i=0;i<chars1.length;i++)
chars[i] = chars1[i];
charElements = chars1.length;
}
void printArray(){ // cost, times
System.out.printf("printArray(%d,%d):
",charElements,chars.length);
for(int i=0; i<charElements;i++)
System.out.print(chars[i]+" ");
System.out.println();
}
int linearSearch(char val) {
for(int i=0;i<charElements;i++)
if(chars[i] == val)
return i;
return -1;
}
int binarySearch(char val) {
int start = 0;
int end = chars.length - 1;
int mid;
while(start <= end) {
mid = (start + end)/2;
if(val == chars[mid])
return mid;
else if(val < chars[mid])
end = mid-1;
else // val > chars[mid]
start = mid + 1;
}
return -1;
}
private void enlarge() {
// char up the size of chars;
char[] new_chars = new char[chars.length*2];
for(int i=0;i<charElements;i++)
new_chars[i] = chars[i];
chars = new_chars;
}
void add(char val) {
if(isFull()) // if(charElements == chars.length)
enlarge();
chars[charElements] = val;
charElements++;
}
public void addOrder(int idx, char[] valArray) {
// add all elements of valArray from the specified position of
chars.
// need to keep order
// eg) [10,20,30] --> addOrder(1,{1,2}) makes
{10,1,2,20,30}
ensureCapacity(charElements + valArray.length);
// Here we can safely assume 'chars' has at least 'charElements +
valArray.length' spaces
for(int i=charElements-1; i>=idx ; i--)
chars[i+valArray.length] = chars[i];
for(int i=0;i<valArray.length;i++)
chars[i+idx] = valArray[i];
charElements += valArray.length;
}
private void ensureCapacity(int count) {
if(count <= chars.length)
return;
// need more space
char[] new_chars = new char[count];
for(int i=0;i<charElements;i++)
new_chars[i] = chars[i];
chars = new_chars;
}
int remove(char val){
// search for an element that is equal to val. and remove it from
'chars'
int idx = linearSearch(val);
if(idx < 0)
return 0;
// fill the location with the last element
chars[idx] = chars[charElements-1];
charElements--;
return 1;
}
// void removeAll(int val) { // worst-case: latter half elements
are equal to val
// // remove(10) in chars = {1, 2, 3, 2, 4, ..., 10, 10, 10,
10}
// // O(N) * N = O(N*N)
// // search for all the elements equal to val and remove
them.
// while(remove(val) > 0);
// }
void removeAll(char val) { // O(N)
int j = 0;
for(int i=0;i<charElements;i++)
if(chars[i] != val)
chars[j++] = chars[i];
charElements = j;
}
char findMin() {
// return the minimum value among elements in chars;
char minV = chars[0];
for(int i=1;i<charElements;i++)
if( chars[i] < minV)
minV = chars[i];
return minV;
}
void sort() { // There is a bug in this code. Find it.
// sort 'chars' in ascending order. eg) 10, 20 , 30
for(int i=0;i<charElements-1;i++) {
int minIdx = i;
for(int j=i+1;j<charElements;j++)
if( chars[j] < chars[minIdx])
minIdx = j;
// swap between chars[i] and chars[minIdx]
char temp = chars[i];
chars[i] = chars[minIdx];
chars[minIdx] = temp;
}
}
char[] toArray() {
// return a copy of the array only with valid elements
//return chars; // this is not a right to return a copy.
char[] new_chars = new char[charElements];
for(int i=0;i<charElements;i++)
new_chars[i] = chars[i];
return new_chars;
}
public MyArrayForChar clone(){
// return a copy of this instance/object
MyArrayForChar chars1 = new MyArrayForChar(this.toArray());
return chars1;
}
public void clear() {
charElements = 0;
}
// In the above class ‘MyArray’, define a new method
‘getElements(int start, int end)’
// that returns a new array. The new array should have
elements
// of ‘chars’ from index ‘start’ to ‘end’ inclusively.
// For example, suppose chars = {10,20,30,40,50}. Then
getElements(2,4)
// returns a new array {30,40,50}. Assume that index ‘start’ and
‘end’ are valid
// (you don’t need to check their validity).
public char[] getElements(int start, int end) {
char[] new_chars = new char[end-start+1];
for(int i= start; i <= end ; i++)
new_chars[i-start] = chars[i];
return new_chars;
}
boolean isEmpty() { return charElements == 0; }
boolean isFull() { return charElements==chars.length; }
}
MyArrayDemo.java
public class MyArrayDemo {
static void printArray(char[] chars){
for(int i=0; i<chars.length;i++)
System.out.print(chars[i]+" ");
System.out.println();
}
public static void main(String[] args) {
MyArrayForChar mychars1 = new MyArrayForChar();
mychars1.add('B');
mychars1.add('A');
mychars1.add('R');
mychars1.add('C');
mychars1.printArray();
System.out.println(mychars1.linearSearch('A'));
System.out.println(mychars1.linearSearch('D'));
mychars1.remove('C');
mychars1.printArray();
char s[] = {'C', 'A', 'D'};
mychars1.addOrder(1,s);
mychars1.printArray();
mychars1.removeAll('A');
mychars1.printArray();
}
}
output screenshot: