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
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 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.
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
JAVA CODE Write a program which has a 3D array and prints the integers from the...
JAVA CODE Write a program which has a 3D array and prints the integers from the array. Then then convert the output into a file using filewriter
write program to compute intersection of two sorted array of integers and compute the CPU time...
write program to compute intersection of two sorted array of integers and compute the CPU time for different sets of unsigned integers generated by a random number generator test this using the same data sets: atleast 3 of size 1000 integers, atleast 3 of size 10000 integers, atleast 3 of size 100000 integers, atleast 3 of one million integers and atleast 3 of size 10 million integers DONT FORGET CPU TIME FOR EACH ONE Java
(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
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Requirements Implement the following functions: Implement a function called readData int *readData( )   The function returns a pointer that points to the locations with integers reading from the file data.txt. arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array...