[Java] Please fix my wordsEndingIn method. You have to use Arrays.copyOf(). You
ID: 3823822 • Letter: #
Question
[Java] Please fix my wordsEndingIn method. You have to use Arrays.copyOf(). You can't use anything like List or clone().
Implement these methods:
public static int min(int[] list) Gets the minimum value in the array. You can assume the array has at least one element
public static int min(ArrayList<Integer> list) Gets the minimum value in the ArrayList as an int. You can assume the ArrayList contains at least one element
public static ArrayList<String> wordsEndingIn(ArrayList<String> list, String letter) Gets an ArrayList of all the words in the given ArrayList that contain the given letter. The comparison is case-insensitive. 'A' and 'a' are counted as the same letter
public static String[] wordsEndingIn(String[] list, String letter) Gets an array of all the words in the given array that contain the given letter. The comparison is case-insensitive. 'A' and 'a' are counted as the same letter. You will need to use Arrays.copyOf() to return an array that contains just the words ending in the letter
[Here's my code and the last method is the problem I'm in]
import java.util.ArrayList;
import java.util.Arrays;
/**
* Static methods working with arrays and arraylists
*/
public class StuffAndThings
{
/**
* Get the minimum number in the array
* @param list an array to work with
* @return min minimum number in the array
*/
public static int min(int[] list)
{
int min = list[0];
for(int i = 0; i < list.length; i++)
{
if(min > list[i])
{
min = list[i];
}
}
return min;
}
public static int min(ArrayList<Integer> list)
{
int min = list.get(0);
for(int i = 0; i < list.size(); i++)
{
if(min > list.get(i))
{
min = list.get(i);
}
}
return min;
}
public static ArrayList<String> wordsEndingIn(ArrayList<String> list, String letter)
{
ArrayList<String> endsWith = new ArrayList<String>();
for(int i = 0; i < list.size(); i++)
{
String check = list.get(i).toLowerCase();
if(check.contains(letter.toLowerCase()))
{
endsWith.add(list.get(i));
}
}
return endsWith;
}
public static String[] wordsEndingIn(String[] list, String letter)
{
String[] str = new String[list.length];
int count = 0;
for(int i = 0; i < list.length; i++)
{
if(!list[i].endsWith(letter))
{
str[i] = str[i - 1];
}
else
{
str[i] = list[i];
}
count++;
}
String[] copy = Arrays.copyOf(str, count);
return copy;
}
}
Explanation / Answer
StuffAndThings.java
import java.util.ArrayList;
import java.util.Arrays;
public class StuffAndThings
{
/**
* Get the minimum number in the array
* @param list an array to work with
* @return min minimum number in the array
*/
public static int min(int[] list)
{
int min = list[0];
for(int i = 0; i < list.length; i++)
{
if(min > list[i])
{
min = list[i];
}
}
return min;
}
public static int min(ArrayList<Integer> list)
{
int min = list.get(0);
for(int i = 0; i < list.size(); i++)
{
if(min > list.get(i))
{
min = list.get(i);
}
}
return min;
}
public static ArrayList<String> wordsEndingIn(ArrayList<String> list, String letter)
{
ArrayList<String> endsWith = new ArrayList<String>();
for(int i = 0; i < list.size(); i++)
{
String check = list.get(i).toLowerCase();
if(check.contains(letter.toLowerCase()))
{
endsWith.add(list.get(i));
}
}
return endsWith;
}
public static String[] wordsEndingIn(String[] list, String letter){
int count = 0;
String endStr[] = new String[0];
for(String s: list){
if(s.endsWith(letter)){
count++;
endStr = Arrays.copyOf(endStr, count);
endStr[count-1]=s;
}
}
return endStr;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.