Sei sulla pagina 1di 10

import java.util.

ArrayList;
import java.util.List;
public class ListOps
{
public static void main(String[] args)
{
List<String> words = new ArrayList<String>();
words.add("yolo");
words.add("westwood");
words.add("mr.");
words.add("russell");
words.add("yolo");
words.add("russell");
words.add("yolo");
words.add("yolo");
// displayList(words);
int matches = countMatchingWords(words, "yolo");
System.out.println("The word appears " + matches + "
times in the list.");
}
public static void displayList(List<String> l)
{
for (int i=0; i<l.size(); i++)
{
System.out.println(l.get(i));
}
}
public static int
findFirstOccurranceIndex(List<String> w, String m)
{
// iterate through list of words, left-to-right
for (int i=0; i<w.size(); i++)
{
// if word found
if (w.get(i).equals(m))
{
return i;

}
}
return -1;
}
public static int findLastOccurranceIndex(List<String>
w, String m)
{
// iterate through list of words, left-to-right
for (int i=w.size()-1; i>=0; i--)
{
// if word found
if (w.get(i).equals(m))
{
return i;
}
}
return -1;
}
public static int countMatchingWords(List<String> w,
String m)
{
int matchingWords = 0;
for (int i=0; i<w.size(); i++)
{
String currentWordInList = w.get(i);
if (currentWordInList.equals(m))
// if (w.get(i).equals(m))
{
matchingWords++;
}
}
return matchingWords;
}
}
_______________________________________________________

public class SearchAndSort


{
public static void main(String[] args)
{
// Remember, binary searching requires elements to be
// in order, so let's set up our test
data set like that.
int[] testArray = {4,5,7,7,10,13,15,23,49,55,71};
// Do a linear search for last instance
of a number
int foundValueIndex = findLast(testArray, 7);
if (foundValueIndex == -1)
System.out.println("Your value was not found!");
else
System.out.println("Your value was found at index " +
foundValueIndex);
foundValueIndex = binarySearch(testArray, 7);
if (foundValueIndex == -1)
System.out.println("Your value was not found!");
else
System.out.println("Your value was found at index " +
foundValueIndex);
}
// Perform a more efficient search - binary search!
public static int binarySearch(int[] a, int val)
{
// Initialize start and end indices of array
int low = 0;
int high = a.length - 1;
// Keep searching until low & mid overlap (or it's
found)
while (low <= high)
{
int mid = (low + high) / 2; // find the midpoint index
between low & high
System.out.println(mid + ", " + a[mid]);

if (a[mid] > val) // Is the search value less than


what's at the midpoint?
{
high = mid - 1; // if so, move the new high value
down to below mid
}
else if (a[mid] < val) // otherwise, is the value
greater than what's at mid?
{
low = mid + 1; // if so, move the new low to above
the current mid
}
else
return mid; // if neither, you found it...return now
b/c you're done
}
return -1;
}

// not found, return -1

/*
public static int findFirst(int[] a, int val)
{
int matchLocationIndex = -1;
// iterate through array a, left-to-right, searching
for val
for (blah; blah; blah)
{
if (blah)
{
blah;
}
}
return matchLocationIndex;
}
*/
// Find LAST occurance of a value in an array
public static int findLast(int[] a, int val)
{

int matchLocationIndex = -1;


// iterate through array a, right-to-left, searching
for val
for (int i=a.length-1; i>=0; i--)
{
if (a[i] == val)
{
return i;
}
}
return matchLocationIndex;
}
}

public class ArrayOps


{
public static void main(String[] args)
{
String[] sentence = new String[4];
sentence[0]
sentence[1]
sentence[2]
sentence[3]

=
=
=
=

"My";
"Westwood";
"is";
"sk00l";

displaySentence(sentence);
// Do the swap
String temp = sentence[1];
sentence[1] = sentence[3];
sentence[3] = temp;
displaySentence(sentence);
// Do the shift
// shiftRight(sentence);
// displaySentence(sentence);

// Do the rotate (right)


// Preserve rightmost value
temp = sentence[sentence.length - 1];
// Shift right
shiftRight(sentence);
// Restore leftmost value
sentence[0] = temp;
displaySentence(sentence);
// Do multiple swaps to get our sentence back!
swap(0, 2, sentence);
swap(0, 1, sentence);
swap(2, 3, sentence);
displaySentence(sentence);
}
// Method to swap elements at index positions a and b
public static void swap(int a, int b, String[]
sentence)
{
String temp = sentence[b];
sentence[b] = sentence[a];
sentence[a] = temp;
}
// Method to shift array elements to the right
public static void shiftRight(String[] sentence)
{
for (int i=sentence.length-1; i>0; i--)
{
sentence[i] = sentence[i-1];
}
}
// Displays all strings as a single sentence
public static void displaySentence(String[] s)
{
for (int i = 0; i < s.length; i++)

{
System.out.print(s[i]);
if (i < s.length - 1)
System.out.print(" ");
}
System.out.println(".");
}
}

public class ArrayRotateRight


{
public static void main(String[] args)
{
char[] array = {'W','e','s','t', 'w', 'o', 'o', 'd'};
displayArray(array);
// 1. Save rightmost element
char temp = array[array.length-1];
// 2. Shift right
shiftRight(array);
displayArray(array);
// 3. Restore element to leftmost position
array[0] = temp;
displayArray(array);
}
// Shift all characters to the right, losing the
rightmost
public static void shiftRight(char[] a)
{
for (int i=a.length-1; i>0; i--)
{
a[i] = a[i-1];
}
}
// Displays all chars as a single word
public static void displayArray(char[] a)

{
for (int i = 0; i < a.length; i++)
{
System.out.print(a[i]);
}
System.out.println("");
}
}

public class ArrayOps {


public static void main(String[] args)
{
String[] sentence = new String[4];
sentence[0] = "My";
sentence[1] =
"Westwood";
sentence[2] =
"is";
sentence[3] = "sk00l";
displaySentence(sentence);
// Do the swap
String temp = sentence[1]; // copies one of the swap
values to temp loc'n.
sentence[1] = sentence[3]; // do one swap
sentence[3] = temp; // restore temp to new position in
sentence
displaySentence(sentence);
/*
// Shift right (must start at right)
for (int i=sentence.length-1; i>0; i--)
{
sentence[i] = sentence[i-1];
}
displaySentence(sentence);
*/
// Rotate left
temp = sentence[0]; // preserve end of line
// shift left (middle part of rotate)
for (int j=0; j<sentence.length-1; j++)
{
sentence[j] = sentence[j+1];
}

// replace element on end with temp


sentence[sentence.length-1] = temp;
displaySentence(sentence);
}
// Custom method to display the contents of the
sentence array
public static void displaySentence(String[] sentence)
{
for (int i=0; i<sentence.length; i++)
{
System.out.print(sentence[i] + " ");
}
System.out.println(".");
}

// Puts us on a new line

public class ArrayFun


{
public static void main(String[] args)
{
// Declare grade array
int[] grades;
// Allocate array - 7 grades
grades = new int[7];
// Initialize grade array
grades[0] = 85;
grades[1] = 95;
grades[2] = 31;
grades[3] = 87;
grades[4] = 89;
grades[5] = 100;
grades[6] = 70;
System.out.println("There are " + grades.length + "
grades in the gradebook.");
int total = 0;

// Iterate through grades


for (int i=0; i<grades.length; i++)
{
// Display grades
System.out.println("Grade " + i + " = " + grades[i]);
// Add current grade to the running total
total += grades[i];
}
// Calculate grade average
int average = total / grades.length;
// Display average
System.out.println("You have an average of " +
average);
}
}

Potrebbero piacerti anche