TestingDocs.com
Software Testing website
  • Automation
    • Selenium
    • JBehave Framework
  • Tutorials
    • MySQL Tutorials
    • Testlink
    • Maven
    • Git
  • IDEs
    • IntelliJ IDEA
    • Eclipse
  • Flowcharts
    • Flowgorithm
    • Raptor
  • About

Java Tutorials

Java List Interface Example

Java

Introduction

Java List interface is one of the most important members of the Java collection framework. The List index starts with 0 like usual arrays in Java. The List interface allows the addition of duplicates and nulls to the list. We can import the List using the following  statement:

import java.util.List;

Sample Listing

Below is a sample java program to capture strings of some length in a list and return the list of strings to the main program.

import java.util.ArrayList;
import java.util.List;

/*-------------------------------------------
 * Program description:
 * To learn how to write a java method that
 * processes a list of strings and returns
 * a sub list of string of specified length
 *
 ---------------------------------------------*/
public class ListOperationsExample {

 /**
 * @param args
 */
 public static void main(String[] args) {
 // Emily Dickenson's poem two lines of
 // first stanza....
 List<String> list= new ArrayList<String>();
 list.add("I");
 list.add("will");
 list.add("tell");
 list.add("you");
 list.add("How");
 list.add("the");
 list.add("Sun");
 list.add("rose");
 list.add(",");
 list.add("A");
 list.add("ribbon");
 list.add("at");
 list.add("a");
 list.add("time");
 
 //Print list
 printList(list,"List");
 //Find string of length 4 in the stanza
 List<String> subList=stringsOfLength(list,4);
 
 //Print sub list
 printList(subList,"SubList");
 }

 /*-------------------------------------------
 * Method description:
 * stringsOfLength method returns a sub list
 * of strings that match the length specified
 * in the method call.
 ---------------------------------------------*/
 public static List<String> stringsOfLength(List<String> list,int n){
 List<String> returnList= new ArrayList<String>();
 int numberOfStrings = list.size();

 for(int i=0;i<numberOfStrings;i++){
 String listString = list.get(i);
 if(listString.length()== n){
 returnList.add(listString);
 }
 }
 return returnList;
 }
 
 /*-------------------------------------------
 * Method description:
 * printList method prints the list
 ---------------------------------------------*/
 public static void printList(List<String> list,String name){
 int numberOfStrings = list.size();
 System.out.print(name + " = [");
 for(int i=0;i<numberOfStrings;i++){
 System.out.print(" " + list.get(i));
 }
 System.out.println("]");
 }
}

Screenshot

 

 

—

Java Tutorials

Java Tutorial on this website:

https://www.testingdocs.com/java-tutorial/

For more information on Java, visit the official website :

https://www.oracle.com/java/

Related Posts

Download Greenfoot Windows

Java Tutorials /

Download & Install Greenfoot on Windows

Java Tutorials /

Java Static Code Analysis

Java Tutorials /

Java Testing Tools

Java Tutorials /

Handle Multiple Exceptions in Java

Exceptions_In_Java

Java Tutorials /

Exceptions in Java Programs

‹ Popular Java IDE Editors› Exceptions in Java Programs

Recent Posts

  • MS Access Data Types
  • Install RAPTOR Avalonia on CentOS
  • Download RAPTOR Avalonia Edition on Windows
  • npm doctor command
  • Build & Run CLion Project
  • Create New CLion C Project on Windows
  • Configure CLion Toolchains on Windows
  • Launch CLion IDE on Windows
  • Activate CLion IDE
  • CLion IDE for C/C++ Development

Back to Top

Links

  • Contact
  • Privacy Policy
  • Cookie Policy

www.TestingDocs.com

Go to mobile version