answersLogoWhite

0

There are several ways to traverse a LinkedList object in Java...

LinkedList<Integer> nums = new LinkedList<Integer>();

// Assume nums is filled with some ints. We don't care which.

// Very inefficient method. This method has a O(n2) efficiency, since each time you call the

// LinkedList.get(n) method, you're iterating through the first n items.

for(int i = 0; i < nums.size(); ++i) {

System.out.println(nums.get(i));

}

// More efficient method. This method has a O(n) efficiency, since you only iterate once per

// item.

for(Iterator<Integer> it = nums.iterator(); it.hasNext();) {

System.out.println(it.next());

}

// Equally efficient as above. Uses some Java syntactic sugar to mask the above

// implementation in a nicer presentation using the for-each loop.

for(Integer n:nums) {

System.out.println(n);

}

User Avatar

Wiki User

16y ago

What else can I help you with?

Related Questions

What are Advantages of HTML Mysql?

MySQL can be linked with HTML easily by including JAR. We will need a Java program to establish an object too.


What do you mean by multithread program in java?

A Program in Java that spawns multiple threads is called a multithreaded program in Java.


In relation to Java what is a thread?

A Java Thread is a thread of execution in a Java Program. A Java Virtual Machine can have a single application running multiple threads, which is known as concurrency. Threads are what make the program run. Each thread has a different priority, and when the machine queue fills up, the threads are executed in the order of their priority.


What is java program on computers?

#!/usr/bin/perl print 'java program';


What is java virtucal mechine?

That refers to the program that runs the compiled Java program.


What powder game doesnt need java?

Any program written for the Java technology needs Java. Any program NOT written for Java DOESN'T need it.


Where can one get the program Java?

The program Java can be downloaded from the official Java website on the internet. The program is free for personal use, so it can be downloaded without any cost by anyone.


How do you write and execute the java program?

In theory, you can write a Java program in just about any text editor; then you can compile it with the "javac" command, and - if the compilation works without errors - run it with the "java" command. In practice, and especially for new programmers, I would recommend that you get a good Java IDE, which lets you do everything in a single environment, and clearly shows where there are errors, among other benefits.


How do you write and execute java program?

In theory, you can write a Java program in just about any text editor; then you can compile it with the "javac" command, and - if the compilation works without errors - run it with the "java" command. In practice, and especially for new programmers, I would recommend that you get a good Java IDE, which lets you do everything in a single environment, and clearly shows where there are errors, among other benefits.


What is a Java applet?

PCH answer small Embedded Java Program.


Do I need Java?

No, if a program needs Java it'll specify that it does in the installer package for the program or on the website for the program or a section usually called "Requirements" for a specific program.


What is a java programming?

A java program is a program that is coded and run in the programming language called java. Java is similar to c++ in structure, and is more common in web apps. C++ is the equivalent for more heavy duty programs such as most software used to compose a java program.