反转字符列表

时间:2018-09-09 02:59:58

标签: javascript

list = [c,a,r,p,e,t]

function reverse(list) {
  var i =0; j= list.length-1;
  while(i < j) {
    var temp = list[i];
    list[i] = list[j];
    list[j] = temp;
    i++;
    j--;
  }
  return list;
}

大家好,我正在尝试解决上述问题。它适用于数字数组。如何调整它以处理字符列表?

4 个答案:

答案 0 :(得分:2)

数组可以通过设计反转,而无需循环:

var list = ['c','a','r','p','e','t'];

var reversedList = list.slice(0).reverse();

检查Mozilla Developer Network: Array.prototype.reverse()Mozilla Developer Network: Array.prototype.slice()以获得更多信息。

答案 1 :(得分:0)

只需使用.reverse();并假设c,a,r,p,e,t是变量,然后使用String()如果要按字母排序,则使用Number()如果按数字排序。

按字符串排序:

var list = [String(c),String(a),String(r),String(p),String(e),String(t)].reverse();

按数字排序:

var list = [Number(c),Number(a),Number(r),Number(p),Number(e),Number(t)].reverse();

参考:.reverse() String() Number(

答案 2 :(得分:0)

您的代码引发-未捕获的ReferenceError:c未定义。当您错过将“或'与字符一起放置时。

更改步骤数组声明步骤:

来自:列表= [c,a,r,p,e,t] 到:list = ['c','a','r','p','e','t'] list = [“ c”,“ a”,“ r “,” p“,” e“,” t“]

您的工作代码

$args= array(
    'post_type' => array( 'post', 'case-study' ),
    's' => $search_term
);

jsfiddle工作示例:

https://jsfiddle.net/m6kt3eus/1/

实现此目标的其他更简单方法: 反转数组的简单方法是javascript中的reverse()

下面是示例代码段:

import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;

public class CountryList {
    public static List<Country> get() {
        // A collection to store our country object
        List<Country> countries = new ArrayList<Country>();

        // Get ISO countries, create Country object and
        // store in the collection.
        String[] isoCountries = Locale.getISOCountries();
        for (String country : isoCountries) {
            Locale locale = new Locale("en", country);
            String iso = locale.getISO3Country();
            String code = locale.getCountry();
            String name = locale.getDisplayCountry();

            if (!"".equals(iso) && !"".equals(code) && !"".equals(name)) {
                countries.add(new Country(iso, code, name));
            }
        }

        // Sort the country by their name and then display the content
        // of countries collection object.
        Collections.sort(countries, new CountryComparator());
        return countries;
    }

    /**
     * Country pojo class.
     */
    public static class Country {
        private String iso;
        private String code;
        private String name;

        Country(String iso, String code, String name) {
            this.iso = iso;
            this.code = code;
            this.name = name;
        }

        public String toString() {
            return iso + " - " + code + " - " + name.toUpperCase();
        }
    }

    /**
     * CountryComparator class.
     */
    private static class CountryComparator implements Comparator<Country> {
        private Comparator<Object> comparator;

        CountryComparator() {
            comparator = Collator.getInstance();
        }

        public int compare(Country c1, Country c2) {
            return comparator.compare(c1.name, c2.name);
        }
    }
}

arr.reverse()用于数组的原位反转。数组的第一个元素变为最后一个元素,反之亦然。

语法:

var list = ['c','a','r','p','e','t']; 
function reverse(list) {
  var i =0; j= list.length-1;
  while(i < j) {
  var temp = list[i];
  list[i] = list[j];
  list[j] = temp;
  i++;
  j--;
}
return list;

}



//Call the method;
reverse(list);

参数 此函数不带任何参数。

返回值 此函数返回反转的原始数组的引用。

答案 3 :(得分:0)

function reverse(list) {
    var list2 = [];
    for (let i = 0; i < list.length; i++)
        list2.unshift(list[i]);
    return list2;
}

var list = ['c', 'a', 'r', 'p', 'e', 't'];
console.log(reverse(list));