Skip to content

Instantly share code, notes, and snippets.

@ckarmann
Forked from danamn/sample.java
Last active November 10, 2016 04:24
Show Gist options
  • Save ckarmann/b5e504ad70466ebfa33d6530ea921200 to your computer and use it in GitHub Desktop.
Save ckarmann/b5e504ad70466ebfa33d6530ea921200 to your computer and use it in GitHub Desktop.
import java.util.Arrays;
public class ArrayMethods
{
String[] list; //instance variable
/**
* Constructor for objects of class ArrayMethods
*/
public ArrayMethods(String[] list)
{
// initialise instance variables
this.list = list;
}
/**
* Determines if the array is sorted (do not sort)
* When Strings are sorted, they are in alphabetical order
* Use the compareTo method to determine which string comes first
* You can look at the String compareTo method in the Java API
* @return true if the array is sorted, else false.
*/
public boolean isSorted()
{
boolean sorted = true;
// TODO: Write the code to loop through the array and determine that each
// successive element is larger than the one before it
// CKA: The idea is good but you have to create a loop, and do this comparison on events from 0 to list.length-1:
// for (int i = 0; i< list.length -1; i++) {
// if (list[i].compareTo(list[i+1]) > 0) { .... }
// }
if(list[0].compareTo(list[1])>0){
sorted=false;
}
return sorted;
}
/**
* Replaces all but the first and last with the larger of its to neighbors
* You can use the compareTo to determine which string is larger (later in alphabetical
* order).
*/
public void replaceWithLargerNeighbor()
{
// CKA: First you have to make a copy of the array at first, or you are going to work on a modified array and
// it is not going to make the wanted effect. See java.util.Arrays.copyOf(...)
// Second, you have to compare between previous and next elements, so do the comparison:
// if (copy[a-1].compareTo(copy[a+1]) < 0) { list[a] = copy[a+1]; } else {list[a] = copy[a-1]; }
for(int a=1;a<list.length-1;a++){
if(list[a].compareTo(list[a+1])<0){
list[a]=list[a+1];
}
}
}
/**
* Gets the number of duplicates in the array.
* (Be careful to only count each duplicate once. Start at index 0. Does it match any of the other element?
* Get the next word. It is at index i. Does it match any of the words with index > i?)
* @return the number of duplicate words in the array.
*/
public int countDuplicates()
{
// CKA: First, you compare String instances with "equals", not with "==".
// Second, after finding the first duplicate for list[a], use "break" to go out of the inner loop.
int duplicates = 0;
for(int a=0;a<list.length-1;a++){
for(int b=a+1;b<list.length;b++){
if(list[a]==list[b]){
duplicates++;
}
}
}
return duplicates;
}
/**
* Moves any word that starts with x, y, or z to the front of the array, but
* otherwise preserves the order
*/
public void xyzToFront()
{
int insertAt = 0;
for(int a=0;a<list.length;a++){
if("xyz".contains(list[a].substring(0,1))){
// CKA: it's good, in particular the idea of decrementing b, except three things:
// - You should move elements to the right from index "a" to "insertAt+1", not from "length-1" to "insertAt+1".
// - insertAt should be incremented not in this loop but in the end of the outer one (after assigning to list[insertAt]).
// - When you come at the list[insertAt] = ..., the value of list[a] has been replaced and thus lost. You have
// to save it in a temporary variable first, and add it to list[insertAt] afterwards.
for (int b=list.length-1;b>insertAt;b--){
list[b]=list[b-1];
insertAt++;
}
list[insertAt]=list[a];
}
}
}
/**
* gets the string representation of this array
* @return a string representation of the array. (do this with Arrays.toString(list))
*/
public String toString()
{
return Arrays.toString(list);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment