/**
*
*/
package groceries;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ShoppingList {
/**
* @param args
*/
public static void main(String[] args) {
List groceryList = new
ArrayList<>();
groceryList.add("rice");
groceryList.add("chicken");
groceryList.add("cumin");
groceryList.add("tomato");
groceryList.add("cilantro");
groceryList.add("lime
juice");
groceryList.add("peppers");
groceryList.remove("cilantro");
for (int i = 0; i
if
(groceryList.get(i).equals("peppers")) {
groceryList.set(i, "yellow peppers");
}
}
System.out.println(groceryList);
// apple, lemon, orange, &
grape juice w/ cinnamon & nutmeg
// remove nutmeg
// add seltzer water
// Loop through and print each word
ending in juice
groceryList.addAll(Arrays.asList("apple juice", "lemon
juice",
"orange juice", "grape juice", "cinnamon",
"nutmeg"));
groceryList.remove("nutmeg");
groceryList.add("seltzer");
for (String grocery : groceryList)
{
if
(grocery.endsWith("juice")) {
System.out.println(grocery);
}
}
}
}
* Add a side dish (elements of it)
* Change "tomato" to "plum
tomato"
* Give a count of all elements
which begin with "C" or "c"
In JAVA
code:
/**
*
*/
package examples;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class ShoppingList {
/**
* @param args
*/
public static void main(String[] args) {
List groceryList = new ArrayList<>();
groceryList.add("rice");
groceryList.add("chicken");
groceryList.add("cumin");
groceryList.add("tomato");
groceryList.add("cilantro");
groceryList.add("lime juice");
groceryList.add("peppers");
groceryList.remove("cilantro");
for (int i = 0; i<groceryList.size();i++) {
if (groceryList.get(i).equals("peppers")) {
groceryList.set(i, "yellow peppers");
}
}
System.out.println(groceryList);
// apple, lemon, orange, & grape juice w/ cinnamon &
nutmeg
// remove nutmeg
// add seltzer water
// Loop through and print each word ending in juice
groceryList.addAll(Arrays.asList("apple juice", "lemon
juice",
"orange juice", "grape juice", "cinnamon",
"nutmeg"));
groceryList.remove("nutmeg");
groceryList.add("seltzer");
for (Object grocery : groceryList) {
if (grocery.toString().endsWith("juice")) {
System.out.println(grocery);
}
}
for (int i = 0; i<groceryList.size();i++) {
if (groceryList.get(i).equals("tomato")) {
groceryList.set(i, "plum tomato");
}
}
System.out.println("After changing tomato to plum tomato:");
for (Object grocery : groceryList) {
System.out.println(grocery.toString());
}
int count=0;
for (Object grocery : groceryList) {
String temp = grocery.toString().toLowerCase();
if(temp.startsWith("c")) {
count++;
}
}
System.out.println("count of all elements which begin with C or c:
"+count);
}
}
Output:
/** * */ package groceries; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ShoppingList { ...
how would i use test.add because i would like to use add import java.io.File; import java.io.FileNotFoundException; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import java.util.Scanner; public class checkFruit { private List<String> tests; public List<String> getFruit(String fruits) { tests = new ArrayList<String>(Arrays.asList(fruits.split(","))); for (String test : tests) { System.out.println(test); tests.add()//how would i use add here } return tests; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter file name: "); File file = new File(in.nextLine()); try { checkFruit...
What is the output of the following program? import java.util.ArrayList; import java.util.Collections; import java.util.List; public class Test implements Comparable<Test> private String[] cast; public Test( String[] st) cast = st; public int compareTo( Test t) if ( cast.length >t.cast.length ) t return +1; else if cast.length < t.cast.length return -1; else return 0; public static void main( String[] args String[] a"Peter", "Paul", "Mary" String[] b_ { "Мое", ''Larry", "Curly", String [ ] c = { ·Mickey", "Donald" }; "Shemp" }; List<Test>...
import java.util.ArrayList; import java.util.List; import java.util.Stack; public class Main { public static void main(String[] args) { int programCounter = 0; List<String> program = new ArrayList<String>(); Stack<String> stack = new Stack<String>(); // TODO string probably not best program.add("GOTO start<<1>>"); program.add("LABEL Read"); program.add("LINE -1"); program.add("FUNCTION Read -1 -1"); program.add("READ"); program.add("RETURN "); program.add("LABEL Write"); program.add("LINE -1"); program.add("FUNCTION Write -1 -1"); program.add("FORMAL dummyFormal 0"); program.add("LOAD 0 dummyFormal"); program.add("WRITE"); program.add("RETURN "); program.add("LABEL start<<1>>"); program.add("LINE 1"); program.add("FUNCTION main 1 4"); program.add("LIT 0 i"); program.add("LIT 0 j");...
JAVA programming 9.9 Ch 9, Part 2: ArrayList Searching import java.util.ArrayList; public class ArrayListSet { /** * Searches through the ArrayList arr, from the first index to the last, returning an ArrayList * containing all the indexes of Strings in arr that match String s. For this method, two Strings, * p and q, match when p.equals(q) returns true or if both of the compared references are null. * * @param s The string...
Java help! Please help complete the min method below in bold. import java.util.Arrays; import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; /** * Provides an implementation of a binary search tree * with no balance constraints, implemented with linked nodes. * * * */ public class Bst<T extends Comparable<T>> implements Iterable<T> { ////////////////////////////////////////////////////////////////// // I M P L E M E N T T H E M I N M E T H O D B E L O W...
import java.util.ArrayList; import java.util.List; public abstract class AbstractBoxPacking { protected List input; protected int boxSize; public AbstractBoxPacking(List input, int boxSize){ this.input = input; this.boxSize = boxSize; } public abstract int getOutput(); public List deepCopy(List boxes){ ArrayList copy = new ArrayList(); for(int i = 0; i < boxes.size(); i++){ copy.add(boxes.get(i).deepCopy()); } return copy; } } I need Help fixing the errors in my java code
Course,java import java.util.ArrayList; import java.util.Arrays; /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ /** * * @author fenghui */ public class Course { private String cName; private ArrayList<Subject> cores; private ArrayList<Major> majors; private ArrayList<Subject> electives; private int cCredit; public Course(String n, int cc){ cName = n; cCredit = cc; cores = new ArrayList<Subject>(0); majors =...
Question 2: Execute the following program. import java.util.Arrays; public class ArrayManipulations { public static void main(String[] args) { // sort doubleArray into ascending order char [] A = {'g', ', 'y', 'a', 'e','d' }; Arrays.sort( A); for (char value: A) System.out.printf("%2C", value); System.out.printf("\n"); char [] A Copy = new char[ 3 ]; System.arraycopy( A, 2, A Copy, 0, A Copy.length); for(char value: A Copy) System.out.printf("%2C", value); System.out.printf("\n"); int location = Arrays.binary Search(A Copy, 'y'); if(location >=0) System.out.printf("y Found at element...
import java.util.List; import java.util.ArrayList; import java.util.LinkedList; public class ListPractice { private static final int[] arr = new int[100000]; public static void main(String[] args) { for(int i=0; i<100000; i++) arr[i] = i; //TODO comment out this line LinkedList<Integer> list = new LinkedList<Integer>(); //TODO uncomment this line //List<Integer> list = new ArrayList<Integer>(); //TODO change the rest of the...
import java.util.Arrays; public class lab { public static void main(String args[]) { int arr[] = {10, 7, 8, 9, 1, 5,6,7}; int arr2[] = {9, 8, 7, 6, 5, 4, 3, 2, 1}; int arr3[] = {1, 3, 5, 3, 2, 6, 20}; quicksort(arr,0,arr.length-1); quicksort(arr2,0,arr2.length-1); quicksort(arr3,0,arr3.length-1); System.out.println(Arrays.toString(arr)); System.out.println(Arrays.toString(arr2)); System.out.println(Arrays.toString(arr3)); } private static int partition(int[] items,int low, int high) { int i=0; int j=0;...