Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- t A:\
- \
- 1. find odd; find even; return array.\
- 2. Linear search \
- 3. int[] array = \{oddCount, evenCount\};\
- return array;\
- 4. No, because the array.length - oddCount = evenCount.\
- 5. \
- public static int[] evenOdds(int[] values)\
- \{\
- int oddCount = 0;\
- int evenCount = 0;\
- for(int e : values) \{\
- if(e % 2 == 0) \{\
- evenCount++;\
- \} else \{\
- oddCount++;\
- \}\
- \
- \}\
- int[] array = \{evenCount, oddCount\};\
- return array;\
- \
- \}\
- \
- Part B:\
- \
- 1. \expnd0\expndtw0\kerning0
- How will you find duplicates? By create 2 variables to record current word and previous word.\
- What will you do when you find them? If current word and previous word are same, remove current word.\
- 2. Yes. \
- 3. Yes.\
- 4. \
- public void removeAdjacentDuplicates()\
- \{\
- String prev = words.get(0);\
- for(int i = 1; i < words.size(); i++) \{\
- String curr = words.get(i);\
- if(prev.equals(curr)) \{\
- words.remove(i);\
- i--;\
- \} else \{\
- prev = curr;\
- \}\
- \}\
- \
- \}\
- 5. Done.\
- \kerning1\expnd0\expndtw0 6. \
- public void removeAllDuplicates() \{\
- ArrayList<String> nodup = new ArrayList<String>();\
- for(String e : words) \{\
- if(! nodup.contains(e)) \{\
- nodup.add(e);\
- \}\
- \}\
- words = nodup;\
- \
- \}\
- \
- 7. Yes.\
- 8. \
- \b This is my original code for the first one
- \b0 \
- public void removeAdjacentDuplicates()\
- \{\
- String prev = words.get(0);\
- for(int i = 1; i < words.size(); i++) \{\
- String curr = words.get(i);\
- if(prev.equals(curr)) \{\
- words.remove(i);\
- i--;\
- \} else \{\
- prev = curr;\
- \}\
- \}\
- \
- \}\
- \
- \b This is the changes that I made to remove all the duplicates
- \b0 \
- public void removeAllDuplicates() \{\
- ArrayList<String> nodup = new ArrayList<String>();\
- for(String e : words) \{\
- if(! nodup.contains(e)) \{\
- nodup.add(e);\
- \}\
- \}\
- words = nodup;\
- \
- \}\
- \
- Part C:\
- \
- 1. Done.\
- 2 . Creates a temporary variable to record the first values.\
- 3. \
- for (int i = 0; i < n; i = i + 2)\
- \{\
- int temp = number[i];\
- numbers[i] = numbers[i + 1];\
- numbers[i + 1] = numbers[i];\
- \}\
- \
- Part D:\
- \
- 1. Done.\
- 2. Done.\
- 3. \
- for(int j = 0; j < list.size()/2; j++) \{\
- Picture temp = list.get(0);\
- for(int i = 0; i < list.size() - 1; i++) \{ \
- list.set(i, list.get(i+1));\
- \}\
- list.set(list.size()-1, temp);\
- \}\
- \
- 4. Done.\
- 5. \
- int mid = list.size()/2;\
- for(int i = 0; i < mid; i++) \{\
- Picture temp = list.get(i + mid);\
- list.set(i + mid, list.get(i));\
- list.set(i, temp);\
- \}\
- \
- \
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement