Ⅰ 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語言