Jump to content

I'm working with an ArrayList and i'm trying to use the isEmpty lmethod, but for some reason the else clause is highlighted as an error and I really have no clue why.

 

Here is the entire program:

package main;

//need to import ArrayList from within java.util package
import java.util.ArrayList; 

public class IntegerArrayListDemo {

	public static void main(String[] args) {
		//create an ArrayList of numbers using the Integer wrapper class
		ArrayList<Integer> list = new ArrayList<Integer>();
		//print list, should be empty
		System.out.println(list.toString());

		//populate with some numbers
		list.add(8);  //auto-boxing, actually: add(new Integer(8)) 
		list.add(32);
		list.add(5);

		//using the other add method
		list.add(1,5);  //inserts 5 at index 1
		list.add(0,104);  //inserts 104 at index 0

		//print list contents
		System.out.println(list.toString());


		//output the current size of the list
		System.out.println("The size is: " + list.size());


		//print element at index 0 using get method
		System.out.println("First element is: " + list.get(0));


		//searches for item (5) and returns index
		int pos = list.indexOf(5);
		System.out.println("Index of 5: (expected 2) result is "  + pos);


		//use contains() to check whether some number is in the list
		if (list.contains(104)) {
			System.out.println("The list contains 104"); //should print this
		}
		else {
			System.out.println("The list does NOT contains 104");
		}
		
		
		//print using a for-i loop with size() and get(i)
		System.out.println("\nUsing standard for-i loop");
		for (int i = 0; i < list.size(); i++) {
			System.out.println(list.get(i));
		}

		//print using for-each loop
		System.out.println("Using for-each loop");
		for (Integer x : list) {
			System.out.println(x);
		}

		//print using forEach method (added in Java 8)
		System.out.println("Using forEach method");
		//passes a lambda expression to the method
		list.forEach(x -> System.out.println(x));


		//TEST OTHER METHODS HERE...
        list.remove(3); //removes element an index 3
        System.out.println(list.toString());
        
        list.set(2, 14); //changes index 2 from element 5 to 14
        System.out.println(list.toString());
        
        list.clear(); //clears all elements from the list
        System.out.println(list.toString());        
        
        if (list.isEmpty());            
           System.out.println(list + " " + "Is empty"); 
           
        else //My IDE is saying this is an error    
           System.out.println(list + " " + "Is not empty");      
	}
}

The bit your looking for is the if else statmement right at the bottom of the program.

 

   
   
Link to comment
https://linustechtips.com/topic/689887-java-arraylists-isempty-method/
Share on other sites

Link to post
Share on other sites

27 minutes ago, GR412 said:

Oh my god, I feel such a plonker right now. Thanks haha.

rip

 

I know that feeling

[Out-of-date] Want to learn how to make your own custom Windows 10 image?

 

Desktop: AMD R9 3900X | ASUS ROG Strix X570-F | Radeon RX 5700 XT | EVGA GTX 1080 SC | 32GB Trident Z Neo 3600MHz | 1TB 970 EVO | 256GB 840 EVO | 960GB Corsair Force LE | EVGA G2 850W | Phanteks P400S

Laptop: Intel M-5Y10c | Intel HD Graphics | 8GB RAM | 250GB Micron SSD | Asus UX305FA

Server 01: Intel Xeon D 1541 | ASRock Rack D1541D4I-2L2T | 32GB Hynix ECC DDR4 | 4x8TB Western Digital HDDs | 32TB Raw 16TB Usable

Server 02: Intel i7 7700K | Gigabye Z170N Gaming5 | 16GB Trident Z 3200MHz

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×