Complete code and answer question in comments:
Package hw4;
import java.util.ArrayList;
public class WordGame {
/*
* Returns all strings that appear
* as a consecutive horizontal or vertical sequence of letters
* (left-right, right-left, up-down, or down-up)
* in the array board and also appear in dict.
* Note that the same word may appear multiple times
* on the board, and will then be multiple times in
* the returned array.
*
* dict is assumed to be in alphabetical order
* board is assumed to be rectangular
*/
public static String[] search(String[] dict, char[][] board) {
/*The following line contains <String>, this means
* that the ret is an ArrayList that contains String. It is an
* ArrayList of only Strings.
*/
ArrayList<String> ret = new ArrayList<String>();
int height = board.length;
// Set width to 0 for an empty board,
// and to the width for first line otherwise,
// because we assume the board is a rectangle
int width = board.length==0 ? 0 : board[0].length;
// TODO Generate substrings of the board
// and check if they are in the dictionary
// by calling inDictionary (which you implement).
// If they are, use ret.add to add them
// to the array that gets returned.
// Character.toString, String+char, and String+=char
// can all be useful here.
// You may want to see https://docs.oracle.com/javase/8/docs/api/java/lang/String.html
// and https://docs.oracle.com/javase/8/docs/api/java/lang/Character.html
//
// TODO (10% of your grade): if your board
// has height h and width w, how many strings
// do you need to check using inDictionary
// (assume that you do nothing to filter out
// duplicates or, equivalently, that the board
// is such that there are no duplicates)
// ANSWER:
// EXPLANATION OF THE ANSWER:
// This line converts ArrayList<String> to String []
return ret.toArray(new String[0]);
}
}
import java.util.ArrayList;
public class WordGame {
public static String[] search(String[] dict, char[][]
board) {
ArrayList<String> ret =
new ArrayList<String>();
int height = board.length;
int width = board.length == 0 ? 0 : board[0].length;
for (int r = 0; r < height; r++) { // row
for (int c =
0; c < width; c++) { // column
String s =
Character.toString(board[r][c]);
if (HW4IO.inDictionary(dict, s))
//checks for one-letter words
ret.add(s);
for (int i = c + 1; i < width; i++) { //
checking to the right--row doesn't change
s += board[r][i];
if (HW4IO.inDictionary(dict,
s)) // check if s is in the dictionary
ret.add(s);
}
s = Character.toString(board[r][c]);
for (int i = c - 1; i >= 0; i--) { // check
to the left
s += board[r][i];
if (HW4IO.inDictionary(dict,
s))
ret.add(s);
}
s = Character.toString(board[r][c]);
for (int i = r + 1; i < height; i++) { //
checking down
s += board[i][c];
if (HW4IO.inDictionary(dict,
s))
ret.add(s);
}
// r does change when you check up and
down
s = Character.toString(board[r][c]);
for (int i = r - 1; i >= 0; i--) { //
checking up
s += board[i][c];
if (HW4IO.inDictionary(dict,
s))
ret.add(s);
}
}
}
// This line converts
ArrayList<String> to String []
return ret.toArray(new
String[0]);
}
}
Complete code and answer question in comments: Package hw4; import java.util.ArrayList; public class WordGame { /*...