Question

4. Write a Java program to test if the first and the last element of an...

4. Write a Java program to test if the first and the last element of an array list of integers are same
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
// Java code to find first and last element 
// of ArrayList 

import java.util.ArrayList; 

public class Main{ 

        // main method 
        public static void main(String[] args) 
        { 

                // creating an Empty Integer ArrayList 
                ArrayList<Integer> list = new ArrayList<Integer>(5); 

                // using add() to initialize values 
                list.add(1); 
                list.add(2); 
                list.add(3); 
                list.add(4); 

                // printing initial value ArrayList 
                System.out.println("ArrayList1: " + list); 

                // find first element 
                int first = list.get(0); 

                // find last element 
                int last = list.get(list.size() - 1); 


                if(first==last)
                    System.out.println("first element and last element are equal");
                else
                    System.out.println("first element and last element are not equal");
                ArrayList<Integer> list1 = new ArrayList<Integer>(5); 

                // using add() to initialize values 
                list1.add(1); 
                list1.add(2); 
                list1.add(3); 
                list1.add(4); 
                
                System.out.println("ArrayList2: " + list); 

                // find first element 
                int first1 = list.get(0); 

                // find last element 
                int last1 = list.get(list.size() - 1); 

                // print first and last element of ArrayList 
                if(first==last && first1==last1)
                    System.out.println("first element and last element are equal in both lists");
                else
                    System.out.println("first element and last element are not equal in both lists");
                
                
        } 
} 

Output:

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
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
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
***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...
UNSORTED VERSION: Write a Java program that uses “brute force” to find the closest pair, given...
UNSORTED VERSION: Write a Java program that uses “brute force” to find the closest pair, given N distinct integer points in 1-dimension. That is, all points are integers on the x-axis, and are in an array. This algorithm should go through all pairs (without sorting the elements of the array), one pair at a time, to find the current closest pair. For example, assume we had the following integers: 4, 2, -2, -1 in an array. We would then generate...
java problem This example creates a program to let a first grader practice additions. write. a...
java problem This example creates a program to let a first grader practice additions. write. a program that randomly generates two single-digit integers number1 and number2 and displays a question such as “What is 7 + 9?” to the student. After the student types the answer, the program displays a message to indicate whether the answer is true or false. use boolean and math random
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT