Ⅰ java 集合中怎么将元素倒序排列
方法一:实现Comparable接口排序package collsort.comparable;
package com.cvicse.sort.comparable;
public class Cat implements Comparable<Cat> {
private int age;
private String name;
public Cat(int age, String name) {
this.age = age;
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
......
public int compareTo(Cat o) {
return this.getAge() - o.getAge();
}
......
}
通过实现Comparable接口实现个性化排序测试。排序测试,Collection.sort(list)升序排列Collections.sort(list, Collections.reverseOrder());降序排列;Collections.reverse(list);反转排序,先输出列表最后一个元素
public class TestComparable {
public static void main(String args[]) {
test();
test2();
}
public static void test() {
......
List<Cat> listCat1 = new ArrayList<Cat>();
Cat cat1 = new Cat(34, "hehe");
Cat cat2 = new Cat(12, "haha");
Cat cat3 = new Cat(23, "leimin");
Cat cat4 = new Cat(13, "lavasoft");
listCat1.add(cat1);
listCat1.add(cat2);
listCat1.add(cat3);
......
System.out.println("调用Collections.sort(List<T> list)listCat2升序排序:");
Collections.sort(listCat1);
System.out.println("降序排列元素:");
Collections.sort(listCat1, Collections.reverseOrder());
System.out.println("Collections.reverse 从列表中最后一个元素开始输出:");
Collections.reverse(listCat1);
......
}
/**
* 针对数组的排序
*/
public static void test2() {
String[] strArray = new String[] { "z", "a", "C" };
System.out.println("数组转换为列表");
List<String> list = Arrays.asList(strArray);
System.out.println("顺序排序列表");
Collections.sort(list);
System.out
.println("按String实现的Comparator对象String.CASE_INSENSITIVE_ORDER排序----");
Collections.sort(list, String.CASE_INSENSITIVE_ORDER);
System.out.println("倒序排序列表");
Collections.sort(list, Collections.reverseOrder());
......
}
}
方法二:实现Comparator接口排序
public class Person {
private int age;
private String name;
......
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
......
}
实现了Comparator接口,重写了compare方法
import java.util.Comparator;
public class PersonComparator implements Comparator<Person> {
public int compare(Person o1, Person o2) {
return o1.getAge() - o2.getAge();
}
}
测试方法
public class TestComparator {
public static void main(String args[]) {
test1();
}
public static void test1() {
System.out.println("升序排序测试:");
List<Person> listPerson = new ArrayList<Person>();
Person person1 = new Person(34, "lavasoft");
Person person2 = new Person(12, "lavasoft");
Person person3 = new Person(23, "leimin");
Person person4 = new Person(13, "sdg");
listPerson.add(person1);
listPerson.add(person2);
listPerson.add(person3);
Comparator<Person> ascComparator = new PersonComparator();
System.out.println("排序后集合为:");
// 利用Collections类静态工具方法对集合List进行排序
Collections.sort(listPerson, ascComparator);
System.out.println("\n降序排序测试:");
// 从升序排序对象产生一个反转(降序)的排序对象
Comparator<Person> descComparator = Collections
.reverseOrder(ascComparator);
System.out.println("利用反转后的排序接口对象对集合List排序并输出:");
Collections.sort(listPerson, descComparator);
outCollection(listPerson);
}
}
Ⅱ C语言编程,编程实现怎样将一个数组逆序输出
数组逆序禅晌输出,只要从数组最后一位向前循环输出即可。
实现代码如下:
#include"stdio.h"
voidmain()
{
inta[100],n,m;
printf("请输入元素的个数:");
scanf("%d",&n);
printf("请依次输入%d个数:",n);
for(m=0;m<n;m++)
scanf("%d",&a[m]);
printf("按逆序输出为:");
for(m=n-1;m>=0;m--)
printf("%d",a[m]);
}
执行结果
C语言是一门通用计算机编程语言,应用广泛。C语言的设计目标是提供一种能以简易的方式编译、处理低级存储器、产生少量的机器码以及不需要任何运早首行环境支持便能运行贺睁锋的编程语言。
Ⅲ 用C语言编程:将一个数组逆序输出
从最后一个元素,培绝逆向遍历到数组的0元素,逐一输出即可实现。
1、创建数组;
2、输入值;
3、逆序遍历输出数组。
代码:int弯中灶main(){ inta[10]; inti;
for(i=0;i<10;i++)
scanf("%d",a+i);
for(i=9;i>=0;i--)
printf("%d",a[i]);}
(3)编程中如何倒序输出列表中的元素扩展阅读:
将一个数组逆序输出。
1、程序分析:用第一个与最后一个交换。
2、程序源代码:
#define N 5
main()
{ int a[N]={9,6,5,4,1},i,temp;
printf(" original array: ");
for(i=0;i printf("%4d",a[i]);
for(i=0;i {temp=a[i];
a[i]=a[N-i-1];
a[N-i-1]=temp;
}
printf(" sorted array: ");
for(i=0;i printf("%4d",a[i]);}
Ⅳ c语言 倒序输出
#include <stdio.h>
int main()
{
int n,i,ret=0;
scanf("%d",&n);
while(n>0){
i=n%10;
ret=ret*10+i;
n /=10;
}
printf("%d",ret);
return 0;
}
(4)编程中如何倒序输出列表中的元素扩展阅读:
起初,C语言没有官方标准。1978年由美国电话电报公司(AT&T)贝尔实验室正式发表了C语言。布莱恩·柯林汉(Brian Kernighan) 和 丹尼斯·里奇(Dennis Ritchie) 出版了一本书,名叫《The C Programming Language》。
这本书被 C语言开发者们称为K&R,很多年来被当作 C语言的非正式的标准说颂冲明。人们称这个版本的野森歼 C语言为K&R C。
K&R C主要介绍了以下特色:
结构体(struct)类型
长整数(long int)类型
无符号整春饥数(unsigned int)类型
把运算符=+和=-改为+=和-=。因为=+和=-会使得编译器不知道使用者要处理i = -10还是i =- 10,使得处理上产生混淆。
即使在后来ANSI C标准被提出的许多年后,K&R C仍然是许多编译器的最 准要求,许多老旧的编译器仍然运行K&R C的标准。
参考资料:网络-c语言