BaşlayınÜcretsiz Başlayın

Working with LinkedList

Now you will create a shopping list as a LinkedList (vs ArrayList) and perform the same types of add/remove of String objects to the shopping list. This allows you to see how LinkedList and ArrayList share the same List interface and are polymorphic in their behavior.

Bu egzersiz

Data Types and Exceptions in Java

kursunun bir parçasıdır
Kursu Görüntüle

Egzersiz talimatları

  • Import LinkedList for use in the application.
  • Construct a new LinkedList of Strings and set the shopList variable to it.
  • Add "eggs" to the end of the shopping list.
  • Add a second "milk" to the beginning of the shopping list.

Uygulamalı interaktif egzersiz

Bu örnek kodu tamamlayarak bu egzersizi bitirin.

// Import LinkedList
import java.____.____;

public class Shopping {

	public static void main(String[] args) {

		// Create a LinkedList of Strings using parameterized constructor
		____<____> shopList = ____ ____<____>();
		shopList.addLast("milk");
        // Add eggs to the end of the list
		shopList.____("____");
		shopList.addLast("bread");
		// Add milk to the beginning of the list again
		shopList.____("____");
		System.out.println(shopList);
		shopList.set(3,"rye-bread");
		shopList.remove("milk");
		System.out.println(shopList);
		System.out.println(shopList.size());
	}
}
Kodu Düzenle ve Çalıştır