Question

Assume we already have an ArrayList of Integers called list. Write code that will add 1 to each element of the list. For exam
0 0
Add a comment Improve this question Transcribed image text
Answer #1

ANSWER: Here I am giving you the code and output please like it.

CODE:

import java.util.ArrayList;
public class Main
{
   public static void main(String[] args) {
       ArrayList<Integer> list = new ArrayList<Integer>();
       list.add(2);
       list.add(4);
       list.add(-3);
       System.out.println("Given list is :"+list);
      
       for(int i=0;i<list.size();i++) {
                   list.add(i, list.get(i)+1);
       }
       System.out.println("ArrayList after adding 1 in each element:"+list);
   }
}

OUTPUT:

Given list is : [2, 4, -3] ArrayList after adding 1 in each element:[3, 5, -2]

Add a comment
Know the answer?
Add Answer to:
Assume we already have an ArrayList of Integers called list. Write code that will add 1...
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
  • JAVA Create an ArrayList called list of type Character 2. Add the following five elements |...

    JAVA Create an ArrayList called list of type Character 2. Add the following five elements | < \ / > (4 points) 3. Print the elements of the list, with a label (3 points) 4. Delete the last element of the list (4 points) 5. Use the method size to determine how many elements there are in the list (no hard-coded numbers). Print the number of elements. (3 points) 6. Add a plus sign ( + ) to the list...

  • 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...

  • A java exercise please! Write a static method named swapHavles that takes an ArrayList of integers...

    A java exercise please! Write a static method named swapHavles that takes an ArrayList of integers as a a parameter and returns a new ArrayList that has every element in the second half of the original ArrayList swapped with every element in the first half of the original ArrayList. Note: • You can assume that the ArrayList passed to this method as a parameter always contains an even number of elements and will always contain at least two elements. For...

  • Please solve this in Java. Assume we have already defined a variable of type int called...

    Please solve this in Java. Assume we have already defined a variable of type int called num with the following line of code: int num = ???; // 'num' can have any int value TASK: Print a Triforce composed of 3 centered triangles created with num asterisks (*) on the bottom row, num - 2 asterisks on the next bottom row, ..., and 1 asterisk on the top row, and such that the triangles are surrounded by hyphens (-). You...

  • (a) Write a C program to print a list of all integers between 1 and 1,000...

    (a) Write a C program to print a list of all integers between 1 and 1,000 (inclusive) which are divisible by 7 but not by 13. The list should be printed to a file called "output.dat". Remember to close the file after use. (35%) (b) Explain what is meant by the C preprocessor. Explain the effect of the following line of code: #define SQUARE (x) (x) * (x) (25%) (c) Explain the concept of an array in C. Write down...

  • Hello, this is java code. I'm practicing my Java and add comments if possible for explanation....

    Hello, this is java code. I'm practicing my Java and add comments if possible for explanation. Question 1: Create an java program that has a class called Square. Create a function called squareParameter that calculates the parameter of the sides of the square. Write test methods in main() to test for different output. Question 2: Create a class called CarYearMade that contains a function called yearMade. This function receives an arraylist of different years. The function should return another arraylist...

  • Write a program that creates a list of 20 integers (between 1 and 40) using a...

    Write a program that creates a list of 20 integers (between 1 and 40) using a random number generator function. Write a function that returns the index (position) of the third largest element from the list of integers. If there are more than one third largest number, return the index of the first occurrence of that number. Eg: Assume that your main function has created the following list of integers generated from the random number function. [12, 30, 28, 4,...

  • Write you code in a class named HighScores, in file named HighScores.java. Write a program that...

    Write you code in a class named HighScores, in file named HighScores.java. Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look approximately like this: Enter the name for score #1: Suzy Enter the score for score #1: 600 Enter the name for score...

  • Write a new program called TrickOr Treat that will do the following 1. In a while...

    Write a new program called TrickOr Treat that will do the following 1. In a while loop, say "Trick or Treat!" and allow the user to type in the name of a candy and store it in an ArrayList. Once the user types "Trick", then the loop should stop. Then, print out the total number of candies on the screen. (See example below) [1 points] 2. Write a method called countSnickers that will take an ArrayList of candy as an...

  • Instructions We're going to create a program which will allow the user to add values to a list, p...

    Instructions We're going to create a program which will allow the user to add values to a list, print the values, and print the sum of all the values. Part of this program will be a simple user interface. 1. In the Program class, add a static list of doubles: private statie List<double> _values new List<double>) this is the list of doubles well be working with in the program. 2. Add ReadInteger (string prompt) , and ReadDouble(string prompt) to the...

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