Question

5. Write a Java program to test if the first and the last element of two...

5. Write a Java program to test if the first and the last element of two array list of integers are same

Homework Answers

Answer #1

Please find the Java code for the following:

Code:

import java.util.ArrayList;
public class ArrayListOperations {
   public static void main(String[] args) {
       //creating two Empty Integer ArrayList
ArrayList<Integer> list1 = new ArrayList<Integer>(5),list2 = new ArrayList<Integer>(5);
  
//We are using add() to initialize values in list1
list1.add(1);
list1.add(2);
list1.add(3);
list1.add(1);
//We are using add() to initialize values in list2
list2.add(4);
list2.add(3);
list2.add(2);
list2.add(1);
  
//Here, we need to Fetch the first and the last element of two array list
  
//Get the first and last elements from list1
//and store them in first1 and last1 variables
int first1=list1.get(0);
int last1=list1.get(list1.size()-1);
  
//Get the first and last elements from list2
//and store them in first2 and last2 variables
int first2=list2.get(0);
int last2=list2.get(list2.size()-1);
  
//Check if the first and the last element of array list of integers are same in list1
if(first1==last1)
   System.out.println("First and last elements are same in First list");
else
   System.out.println("First and last elements are not same in First list");
  
//Check if the first and the last element of array list of integers are same in list2
if(first2==last2)
   System.out.println("First and last elements are same in Second list");
else
   System.out.println("First and last elements are not same in Second list");
   }
}
Please check the compiled program and its output for your reference:

Output:

(I believe that I made the code simple and understandable. If you still have any query, Feel free to drop me a comment)


Hope this Helps!!!
Please upvote as well, If you got the answer?
If not please comment, I will Help you with that...

Know the answer?
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
Similar Questions
Java Program Write a method called “fillMatrix” that takes two integers rows and cols and returns...
Java Program Write a method called “fillMatrix” that takes two integers rows and cols and returns a 2D array of integers of size rows x cols where the value of element [row][col] is row + col. Write a short program to test your method.
Write a C program that finds the last and first element in the array.
Write a C program that finds the last and first element in the array.
java Write a single Java statements to accomplish each of the following: a) Displaythevalueoftheseventhelementofcharacterarraych. b) Considering...
java Write a single Java statements to accomplish each of the following: a) Displaythevalueoftheseventhelementofcharacterarraych. b) Considering “Scanner in = new Scanner (System.in);”, input a value into element 5 of one-dimensional double array nums. c) Initialize each of the four elements of the one-dimensional integer array test to 7. d) Declare and create an array called table as a float array that has four rows and three columns. e) Considering the following ArrayList declaration, insert “test” at the fourth position (index...
Write a Java program that asks the user to enter an array of integers in the...
Write a Java program that asks the user to enter an array of integers in the main method. The program should prompt the user for the number of elements in the array and then the elements of the array. The program should then call a method named isSorted that accepts an array of and returns true if the list is in sorted (increasing) order and false otherwise. For example, if arrays named arr1 and arr2 store [10, 20, 30, 41,...
Write a Java program to get the difference between the largest and smallest values in an...
Write a Java program to get the difference between the largest and smallest values in an array of integers. The length of the array must be 1 and above
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of...
(C++) 5.15 LAB: Two smallest numbers with arrays Write a program that reads a list of integers, and outputs the two smallest integers in the list, in ascending order. The input begins with an integer indicating the number of integers that follow. Ex: If the input is: 5 10 5 3 21 2 the output is: 2 3 You can assume that the list of integers will have at least 2 values. To achieve the above, first read the integers...
Write a complete Java program to solve the following problem. Read two positive integers from the...
Write a complete Java program to solve the following problem. Read two positive integers from the user and print all the multiple of five in between them. You can assume the second number is bigger than the first. For example if the first number is 1 and the second number is 10, then your program should output 5 10 Note: There is a white space in between the numbers. Java programming please answer ASAP before 2:30
JAVA Write a program that has two functions in which the user chooses which one to...
JAVA Write a program that has two functions in which the user chooses which one to perform; 1. reads in a CSV file of integers into an array and insert it into a binary search tree class 2. deserializes an object and inserts it into a binary search tree class the tree class must be in a separate class from the CSV file read and deserialize object load
Write a program that uses an array of integers initialized to whatever values you wish. Write...
Write a program that uses an array of integers initialized to whatever values you wish. Write a methods to calculate and return the minimum and a method to calculate and return the maximum value in the array. Write an additional method that accepts the array as a parameter and then creates and returns a new array with all the same values as the original plus 10. (num +=10) In Java
Write a C program to combine two arrays of the same size arranged in order descendant....
Write a C program to combine two arrays of the same size arranged in order descendant. Test data : Enter the number of elements to be stored in the first array: 3 Input 3 elements in the arrangement: element [0]: 1 element [1]: 2 element [2]: 3 Enter the number of elements to be stored in the second array: 3 Input 3 elements in the arrangement: element [0]: 1 element [1]: 2 element [2]: 3 Expected output: The combined array...