Advertisement
Guest User

Untitled

a guest
Jun 30th, 2015
186
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. /**********************************************************
  2. *
  3. * 08-722 Data Structures for Application Programmers
  4. * Homework 1 My Array implementation
  5. *
  6. *
  7. **********************************************************/
  8. public class MyArray{
  9. private String[] text;
  10. private int numOfWords;
  11.  
  12. public MyArray(int capacity){
  13. text = new String[capacity];
  14. numOfWords=0;
  15. }
  16.  
  17. //helper function, add an element to text
  18. private void append(String str){
  19. text[numOfWords]=str;
  20. numOfWords++;
  21. }
  22.  
  23. private void checkCapacity(){
  24. if ( numOfWords+1 <= text.length){
  25. return;
  26. }
  27. int newCapacity;
  28. // if there is no word in the text, initialize capacity as 1
  29. if(text.length==0){
  30. newCapacity=1;
  31. }else{
  32. newCapacity=text.length*2;
  33. }
  34. //update the data from oldtext[] to newtext[]
  35. String[] newtext = new String[newCapacity];
  36. System.arraycopy(text, 0, newtext, 0, numOfWords);
  37. text=newtext;
  38. }
  39.  
  40. //check whether a word is a sequence of letters [a..z]
  41. //private helper function
  42.  
  43.  
  44.  
  45. public void add(String str){
  46. if(str == null){
  47. return;
  48. }
  49. if(!isWord(str) || str.isEmpty()){
  50. return;
  51. }
  52. checkCapacity();
  53. append(str);
  54. return;
  55. }
  56.  
  57. private boolean isWord(String str){
  58. char c;
  59. for(int i=0;i< str.length();i++){
  60. c = str.charAt(i);
  61. if( c == '_' || c == ' ')
  62. return false;
  63. if( c <='9' && c >='0')
  64. return false;
  65. if ((c <='z' && c >='a') || (c<='Z' && c >= 'A'))
  66. continue;
  67. else
  68. return false;
  69. }
  70. return true;
  71. }
  72.  
  73. public boolean search(String keyword){
  74. for (int i=0;i<numOfWords;i++){
  75. if (text[i].equals(keyword)){
  76. return true;
  77. }
  78. }
  79. return false;
  80. }
  81.  
  82. public void display(){
  83. for(int i=0;i<numOfWords-1;i++){
  84. System.out.print(text[i]);
  85. System.out.print(' ');
  86. }
  87. System.out.println(text[numOfWords-1]);
  88. }
  89.  
  90. private boolean isFind(String word, String[] newString){
  91. for(int i=0; i < newString.length;i++){
  92. //the rest of newString are all null
  93. if(newString[i] == null)
  94. return false;
  95. else{
  96. if(newString[i].equals(word))
  97. return true;
  98. }
  99. }
  100. //does not find word in newString, then return false
  101. return false;
  102. }
  103.  
  104. public void removeDups(){
  105. String[] newString = new String[numOfWords];
  106. int count = 0;
  107. for(int i=0;i< numOfWords ;i++){
  108. if(!isFind(text[i],newString)){
  109. newString[count] = text[i];
  110. count++;
  111. }
  112. }
  113. text = new String[text.length];
  114. numOfWords=count;
  115. System.arraycopy(newString, 0, text, 0,numOfWords);
  116. }
  117.  
  118. public int size(){
  119. return numOfWords;
  120. }
  121.  
  122. public int getCapacity(){
  123. return text.length;
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement