Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
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 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)
{
=
=
=
=
"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);
{
System.out.print(s[i]);
if (i < s.length - 1)
System.out.print(" ");
}
System.out.println(".");
}
}
{
for (int i = 0; i < a.length; i++)
{
System.out.print(a[i]);
}
System.out.println("");
}
}