从逗号分隔整数的字符串数组映射的连续天数

我收到了多个以逗号分隔的字符串整数作为输入,例如以下字符串:

  1. “5,6,0”

  2. “0,1,2”

  3. “1,2,3,4”

这些整数中的每一个都代表一周中的一天

  • 0 = 星期日 1 = 星期一 2 = 星期二 3 = 星期三 4 = 星期四 5 = 星期五 6 = 星期六

对于第一个字符串,这意味着星期四星期日 第二个字符串的有效期是星期日星期二 第三个字符串的有效期是星期一星期四

目前,我正在使用以下

  private fun mapOfDays(validDays: String): LinkedHashMap<Int, String>

    {

        if (!validDays.isBlank())

        {

            val daysArray = validDays.split("\\s*,\\s*") as Array<String>

            var mapDays = LinkedHashMap<Int, String>()

            var mapDay = LinkedHashMap<Int, String>()

            mapDays[0] = "SUNDAY"

            mapDays[1] = "MONDAY"

            mapDays[2] = "TUESDAY"

            mapDays[3] = "WEDNESDAY"

            mapDays[4] = "THURSDAY"

            mapDays[5] = "FRIDAY"

            mapDays[6] = "SATURDAY"


            for (day in daysArray)

            {

                if (mapDays.containsKey(day.toInt()))

                {

                    mapDay[day.toInt()] = mapDays[day.toInt()]!!

                }

            }

            return mapDay

        }

        return LinkedHashMap()

    }


    private fun mappedDays(mapOfDays: LinkedHashMap<Int, String>?): String

    {

        if (!mapOfDays.isNullOrEmpty())

        {

            val mapSize = mapOfDays.size


            if (mapSize > 6) return "All Day"

            if (mapSize > 5) return sixDayString(mapOfDays)

            if (mapSize > 4) return fiveDayString(mapOfDays)

            if (mapSize > 3) return fourDayString(mapOfDays)

            if (mapSize > 2) return threeDayString(mapOfDays)

            if (mapSize > 1) return twoDayString(mapOfDays)

            if (mapSize > 0) return oneDayString(mapOfDays)

        }

        return ""

    }

但是,我当前的实现能够告诉我包含哪些天,但无法绘制出天数组,例如:


如果我得到“0,1,3,4,5,6”我想要的最终字符串输出如下:星期三到星期一


或者


“0,1,3,4,5”将导致以下结果:Sunday , Monday , Wednesday to Friday。


慕尼黑8549860
浏览 116回答 2
2回答

Qyouu

package daysimport java.lang.IllegalArgumentExceptionclass DaysFactory {&nbsp; &nbsp; fun dayFromInt(index: Int): Day {&nbsp; &nbsp; &nbsp; &nbsp; return when (index) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 0 -> Day.Sunday&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 -> Day.Monday&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2 -> Day.Tuesday&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3 -> Day.Wednesday&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4 -> Day.Thursday&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5 -> Day.Friday&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 6 -> Day.Saturday&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else -> throw IllegalArgumentException("illigal index :$index")&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; enum class Day(val index: Int) {&nbsp; &nbsp; &nbsp; &nbsp; Sunday(0), Monday(1), Tuesday(2), Wednesday(3), Thursday(4), Friday(5), Saturday(6)&nbsp; &nbsp; }}class DaysRange(val seed: String) {&nbsp; &nbsp; var stringFormat = ""&nbsp; &nbsp; private fun getTomorrow(dayIndex: Int): Int {&nbsp; &nbsp; &nbsp; &nbsp; if (dayIndex != 6) return dayIndex + 1&nbsp; &nbsp; &nbsp; &nbsp; return 0&nbsp; &nbsp; }&nbsp; &nbsp; override fun toString(): String =stringFormat&nbsp; &nbsp; init {&nbsp; &nbsp; &nbsp; &nbsp; if (isValidInput(seed)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val dayFactory = DaysFactory()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val indexes = seed.split(",").map { it.toInt() }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val days = indexes.map { dayFactory.dayFromInt(it) }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; val ranges = splitIndexesToRanges(indexes)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ranges.forEach { range ->&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (range.size > 2) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringFormat += "${dayFactory.dayFromInt(range.first())} to ${dayFactory.dayFromInt(range.last())},"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; range.forEach {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringFormat += "${dayFactory.dayFromInt(it)},"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; stringFormat = stringFormat.dropLast(1)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private fun splitIndexesToRanges(daysRange: List<Int>): ArrayList<List<Int>> {&nbsp; &nbsp; &nbsp; &nbsp; val result = ArrayList<List<Int>>()&nbsp; &nbsp; &nbsp; &nbsp; val slicePoint = ArrayList<Int>()&nbsp; &nbsp; &nbsp; &nbsp; for (i in 0 until daysRange.size - 1) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (getTomorrow(daysRange[i]) != daysRange[i + 1]) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; slicePoint.add(i)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; var start = 0&nbsp; &nbsp; &nbsp; &nbsp; slicePoint.forEach {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.add(daysRange.slice(start..it))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; start = it + 1&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; result.add(daysRange.slice(start until daysRange.size))&nbsp; &nbsp; &nbsp; &nbsp; return result&nbsp; &nbsp; }}private fun isValidInput(seed: String): Boolean = truefun main(args: Array<String>) {&nbsp; &nbsp; val input = listOf(&nbsp; &nbsp; &nbsp; &nbsp; "0,1,2,4,5,6",&nbsp; &nbsp; &nbsp; &nbsp; "5,6,0",&nbsp; &nbsp; &nbsp; &nbsp; "1,2,3,4"&nbsp; &nbsp; )&nbsp; &nbsp; input.forEach {&nbsp; &nbsp; &nbsp; &nbsp; val dr = DaysRange(it)&nbsp; &nbsp; &nbsp; &nbsp; println(dr)&nbsp; &nbsp; }}示例输出:周日至周二、周四至周六周五至周日周一至周四

哈士奇WWW

如果可以的话,我会坚持使用给定的时间 API(例如,java.time如果您使用 Java 8 或 joda-time 等)。以下解决方案也适用于您的enum,但您需要对其进行一些调整(即DayOfWeek具有getDisplayName并且还允许添加单日并始终获得下一个连续天)。我将工作分成 3 个独立的任务。将输入读入以下列表DayOfWeek:fun readInput(input : String) : List<DayOfWeek> = input.splitToSequence(",")&nbsp; &nbsp; .map(String::toInt)&nbsp; &nbsp; .map {&nbsp; &nbsp; &nbsp; /* your 0 is Sunday which is 7 for DayOfWeek; rest is the same */&nbsp; &nbsp; &nbsp; if (it == 0) 7 else it&nbsp; &nbsp; }&nbsp; &nbsp; .map(DayOfWeek::of)&nbsp; &nbsp; .toList()也许您想添加.distinct().sorted()它或想事先验证输入...这取决于您真正想确保什么...将星期几转换为连续天的列表:fun List<DayOfWeek>.toDayRangeList() : List<DayRange> = fold(mutableListOf<DayRange>()) { consecutiveDaysList, day ->&nbsp; consecutiveDaysList.apply {&nbsp; &nbsp; lastOrNull()?.takeIf { it.to + 1 == day }?.apply {&nbsp; &nbsp; &nbsp; to = day&nbsp; &nbsp; } ?: add(DayRange(day))&nbsp; }}为此,我还引入了一个DateRange-class 以便轻松改变结束日期...您也可以使用不可变对象执行此操作,但我发现这种方式更容易。它还DateRange包括一些辅助方法,可以轻松获取所需形式的实际日期(在我的示例中FULL_STANDALONE):data class DayRange(var from: DayOfWeek, var to: DayOfWeek = from) {&nbsp; private fun DayOfWeek.toFullString(locale : Locale) = getDisplayName(TextStyle.FULL_STANDALONE, locale)&nbsp; fun toString(locale : Locale) : String = when (from) {&nbsp; &nbsp; // TODO add missing locale specific strings!&nbsp; &nbsp; to -> from.toFullString(locale)&nbsp; &nbsp; to + 1 -> "All day"&nbsp; &nbsp; else -> "${from.toFullString(locale)} to ${to.toFullString(locale)}"&nbsp; }&nbsp; // just for convenience we use our custom toString-function:&nbsp; override fun toString() = toString(Locale.getDefault())}可选地“展平”列表,即如果最后一天和第一天是连续的,则将它们合并到一个范围中。当我们直接处理时,DayOfWeek我们可以简单地添加另一天并比较两天,无论其中一天是否是一周的最后一天:fun List<DayRange>.flatten(): List<DayRange> {&nbsp; if (size > 1) {&nbsp; &nbsp; val first = first()&nbsp; &nbsp; val last = last()&nbsp; &nbsp; if (last.to + 1 == first.from)&nbsp; &nbsp; &nbsp; return dropLast(1).drop(1)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .toMutableList()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .apply {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; add(DayRange(last.from, first.to))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; }&nbsp; return this}将它们放在一起/演示:listOf("1", "1,2", "1,0", "1,2,3", "1,2,4,5", "1,2,4,5,0", "1,2,3,4,5,6,0", "2,3,4,5,6,0,1")&nbsp; &nbsp; &nbsp; .forEach { input ->&nbsp; &nbsp; &nbsp; &nbsp; print(input)&nbsp; &nbsp; &nbsp; &nbsp; readInput(input)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .toDayRangeList()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .flatten()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .joinToString(", ")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .also {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; println("-> $it")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }打印以下内容:1 -> Monday1,2 -> Monday to Tuesday1,0 -> Sunday to Monday1,2,3 -> Monday to Wednesday1,2,4,5 -> Monday to Tuesday, Thursday to Friday1,2,4,5,0 -> Thursday to Friday, Sunday to Tuesday1,2,3,4,5,6,0 -> All day2,3,4,5,6,0,1 -> All day
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java