bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java LinkedList clone() Method

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java LinkedList clone() Method?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ java.util.LinkedList;
3Order

Put the learning moves in the order that makes the concept easiest to apply.

The clone() method returns a copy of the LinkedList as an Object .
Definition and Usage
Java LinkedList clone() Method

❮ LinkedList Methods

Example

import java.util.LinkedList;
public class Main {
  public static void main(String[] args) {
    LinkedList<String> cars = new LinkedList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    LinkedList cars2 = (LinkedList)cars.clone();
    cars2.set(0, "Toyota");
    System.out.println(cars);
    System.out.println(cars2);
  }
}

Definition and Usage

The clone() method returns a copy of the LinkedList as an Object .

This creates a "shallow" copy, which means that copies of objects in the list are not created, instead the list has references to the same objects that are in the original list.

Note

Since the return type is Object , it must be type casted in order to use it as an LinkedList as shown in the example above.

Syntax

public Object clone()

Technical Details

Returns:A copy of the LinkedList object.

Previous

Java LinkedList clear() Method

Next

Java LinkedList contains() Method