Question

1. Using the ArrayList add() method, write the statement to insert “July” in the first position...

1. Using the ArrayList add() method, write the statement to insert “July” in the first position of the months ArrayList. Describe what would happen if the position number is invalid for the ArrayList.

2. Declare an ArrayList that declares a list of strings with a capacity of 20.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

for adding the element in a position of the array list, if we give an invalid position number (which is greater than it's capacity or less than 0) then it will through an exception saying java.lang.IndexOutOfBoundException, and program terminates.

code:

import java.util.ArrayList;

class Main {

public static void main(String[] args) {

ArrayList months = new ArrayList<String>();

//inserting into the first position

months.add(0, "july");

System.out.println(months.get(0));

//declaring array list with capacity 20

ArrayList list = new ArrayList<String>(20);

}

}

Add a comment
Know the answer?
Add Answer to:
1. Using the ArrayList add() method, write the statement to insert “July” in the first position...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • 2a) Write a method countEvens that takes an ArrayList of String objects as input and returns...

    2a) Write a method countEvens that takes an ArrayList of String objects as input and returns the number of even length strings contained in the input. For example, if the input is [ one, peach, pear, plum ] then countEvents(inp) should return 2. 2b) Write a method, mirror, that doubles the size of a list of integers by appending a mirror image of the list. For example, given an array list containing [ 1, 5, 2, 6 ], the method...

  • Add the following methods to the ArrayList class that we wrote during lecture. You may call...

    Add the following methods to the ArrayList class that we wrote during lecture. You may call the existing methods in ArrayList if you want, but do not use anything from the built-in java.util.ArrayList class. 1. (3 pts) Write a new method named addAll(ArrayList anotherList) that adds all the elements in anotherList to the back of the calling list. Be sure to reallocate the data array if necessary. anotherList should not be modified. 2. (4 pts) Write a new method named...

  • 10.3 Example. When you first declare a new list, it is empty and its length is...

    10.3 Example. When you first declare a new list, it is empty and its length is zero. If you add three objects—a, b, and c—one at a time and in the order given, to the end of the list, the list will appear as a b c The object a is first, at position 1, b is at position 2, and c is last at position 3.1 To save space here, we will sometimes write a list’s contents on one...

  • Assume we already have an ArrayList of Integers called list. Write code that will add 1...

    Assume we already have an ArrayList of Integers called list. Write code that will add 1 to each element of the list. For example, if the list originally contained 2, 4,-3, then it should contain 3, 5, -2 after the lines of code execute. Just write the necessary lines of code (not a whole method or program). Assume we already created the ArrayList and filled it with Integers.

  • Question 1 To a client, an array and an ArrayList are the same type of object....

    Question 1 To a client, an array and an ArrayList are the same type of object. True False Flag this Question Question 2 In what package is ArrayList found? java.lang java.util java.awt java.io Flag this Question Question 3 In an ArrayList, what does the size() method tell a client? How many objects are in the ArrayList How much space is left to hold objects in the ArrayList The maximum capacity of the ArrayList The number of times the add() method...

  • question 27’s end is in the next page 24 Suppose you have created a list using the statement ArrayList String> NYIT CS Courses new ArrayList(31 What would java do when you try to insert more...

    question 27’s end is in the next page 24 Suppose you have created a list using the statement ArrayList String> NYIT CS Courses new ArrayList(31 What would java do when you try to insert more than 31 courses? ANS: LinelI) check one with x: continue process as usual I Ijava automatically expand array space you manually expand more array spaces 25 Suppose NYIT CS COurscs is an object of the class Array List Suring Write Java statements that will display...

  • Input the data for node 1 1 Enter more yes/Kno ? n Display Insert First Insert...

    Input the data for node 1 1 Enter more yes/Kno ? n Display Insert First Insert Last Insert Anywhere Delete First Delete Last Delete Anyone Search Exit Enter your Choice 1 1) 2> 3> 4X 5> 6) 7> 8) Data at 1 1 - Instructions: 1. Write a program to manage following operations Dynamic link list using C language: on a Display List a. Make sure to declare Link List Start pointer inside the main function and pass the same...

  • *JAVA* Assume we already have an ArrayList of Integers called list. Write code that will add...

    *JAVA* Assume we already have an ArrayList of Integers called list. Write code that will add 1 to each element of the list. For example, if the list originally contained 2, 4, -3, then it should contain 3, 5, -2 after the lines of code execute. Just write the necessary lines of code (not a whole method or program). Assume we already created the ArrayList and filled it with Integers.

  • Write a method that given a non-empty ArrayList of Integers parameterand also an Integer parameter, the...

    Write a method that given a non-empty ArrayList of Integers parameterand also an Integer parameter, the method returns a new ArrayList containing the elements from the original ArrayList that come after the first occurrence of Integer parameter in the original ArrayList. If the original ArrayList does not contain an occurrence of the Integer parameter return an empty ArrayList . The original ArrayList must be unaffected by the method execution. Following are some examples of the invocation of the method using...

  • JAVA In this PoD you will use an ArrayList to store different pet names (there are...

    JAVA In this PoD you will use an ArrayList to store different pet names (there are no repeats in this list). This PoD can be done in your demo program (where your main method is) – you don’t have to create a separate class for today. Details Create an arraylist of Strings, then using a Scanner object you will first read in a number that will tell you how many pet names (one word) you will add to the arraylist....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT