Here you can find the source of listOfItems(List
Parameter | Description |
---|---|
items | a parameter |
separator | a parameter |
finalConjunction | a parameter |
public static String listOfItems(List<String> items, String separator, String finalConjunction)
//package com.java2s; /*//from w w w.j a va2 s.c o m * Copyright 2006-2016, Rathravane LLC * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.List; public class Main { /** * Return a string that is a list of items separated by separator and using * the final conjunction. For example, { "A", "B", "C" } --> "A, B, and C" * @param items * @param separator * @param finalConjunction * @return a list of items */ public static String listOfItems(List<String> items, String separator, String finalConjunction) { final int size = items.size(); if (size < 1) return ""; switch (size) { case 1: return items.iterator().next(); case 2: return items.get(0) + " " + finalConjunction + " " + items.get(1); default: { final StringBuffer result = new StringBuffer(); for (int i = 0; i < size - 1; i++) { result.append(items.get(i)); result.append(separator); } result.append(finalConjunction); result.append(" "); result.append(items.get(size - 1)); return result.toString(); } } } }