数据结构LIst

简介: 数据结构LIst

第一题

某校科技有若干个学生,(学生对象放在一个LIst当中),每个学生有一个姓名,班级和某次考试成绩的属性(double),某次考试结束后,每个学生都获得了一个考试成绩。遍历LIst集合,并把学生对象的属性打印出来

import java.util.ArrayList;
class Student {
    private String name;
    private String classes;
    private double score;
    public Student(String name, String classes, double score) {
        this.name = name;
        this.classes = classes;
        this.score = score;
    }
    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", classes='" + classes + '\'' +
                ", score=" + score +
                '}';
    }
}
public class Main {
    public static void main(String[] args) {
        ArrayList<Student> students = new ArrayList<>();
        students.add(new Student("gaobo", "102", 18.3));
        students.add(new Student("gaobo1", "102", 128.3));
        students.add(new Student("gaobo1", "102", 138.3));
        System.out.println(students);
    }
}

分析代码可以知道,ArrayList中存放的是Student这个对象,所以每次添加的时候都要new一个新的学生对象。

第二题

有一个List当中存放的是整型数据,要求使用Collection.sort进行排序

import java.util.ArrayList;
import java.util.Collections;
public class Main {
    public static void main(String[] args) {
        ArrayList<Integer> integers = new ArrayList<>();
        integers.add(22);
        integers.add(2);
        integers.add(33);
        Collections.sort(integers);
        System.out.println(integers);
    }
}

这题int型就放int对应的类,直接调用Collection方法下面的sort类就行了

第三题

删除第一个字符串当中出现的第二个字符串中的字符

String str1 = “welcome to bit”;

String str2 = “come”;

输出结果:

wl t bit

用String解决

import java.util.Scanner;
public class Main {
    public static String fun(String str1, String str2) {
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < str1.length(); i ++) {
            char ch = str1.charAt(i);
            if(! str2.contains(ch+"")) {
                sb.append(ch);
            }
        }
        return sb.toString();
    }
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String str1 = scanner.nextLine();
        String str2 = scanner.nextLine();
        System.out.println(fun(str1, str2));
    }
}

用ArrayList解决

import java.util.ArrayList;
public class Main {
    public static void main(String[] args) {
        String str1 = "welcome to bit";
        String str2 = "come";
        ArrayList<Character> list = new ArrayList<>();
        for(int i = 0; i < str1.length(); i ++) {
            char ch = str1.charAt(i);
            if(! str2.contains(ch+"")) {
                list.add(ch);
            }
        }
        for(char ch : list) {
            System.out.print(ch);
        }
    }
}

第四题

打印一幅扑克牌

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
class Card {
    private String name;
    private int rank;
    public Card(String name, int rank) {
        this.name = name;
        this.rank = rank;
    }
    @Override
    public String toString() {
        return "[ " + this.name + " " + this.rank + " ]";
    }
}
public class Main {
    public static List<Card> buyCard() {
        String[] suits = {"♥", "♠", "♦", "♣"};
        ArrayList<Card> cards = new ArrayList<>();
        for(int i = 0; i < 4; i ++) {
            for(int j = 1; j <= 13; j ++) {
                cards.add(new Card(suits[i], j));
            }
        }
        return cards;
    }
    public static void swap(List<Card> cards, int i, int j) {
        Card tmp = cards.get(i);
        cards.set(i, cards.get(j));
        cards.set(j, tmp);
    }
    public static void buffCard(List<Card> cards) {
        int size = cards.size();
        for(int i = size - 1; i > 0; i --) {
            Random random = new Random();
            int ran = random.nextInt(i);
            swap(cards, i, ran);
        }
    }
    public static void main(String[] args) {
        List<Card> cards = buyCard();
        System.out.println("买牌" + cards);
        buffCard(cards);
        System.out.println("洗牌" + cards);
        ArrayList<List<Card>> hand = new ArrayList<>();
        ArrayList<Card> hand1 = new ArrayList<>();
        ArrayList<Card> hand2 = new ArrayList<>();
        ArrayList<Card> hand3 = new ArrayList<>();
        hand.add(hand1);
        hand.add(hand2);
        hand.add(hand3);
        for(int i = 0; i < 5; i ++) {
            for(int j = 0; j < 3; j ++) {
                hand.get(j).add(cards.remove(0));
            }
        }
        System.out.println("第一个人的牌:" + hand1);
        System.out.println("第二个人的牌:" + hand2);
        System.out.println("第三个人的牌:" + hand3);
        System.out.println("剩下的牌:" + cards);
    }
}


相关文章
|
存储 NoSQL Java
使用 Redis 的 List 数据结构实现分页查询的思路
使用 Redis 的 List 数据结构实现分页查询的思路
|
消息中间件 NoSQL Redis
redis数据结构-List
redis数据结构-List
384 1
|
存储 消息中间件 NoSQL
【Redis】常用数据结构之List篇:从常用命令到典型使用场景
本文将系统探讨 Redis List 的核心特性、完整命令体系、底层存储实现以及典型实践场景,为读者构建从理论到应用的完整认知框架,助力开发者在实际业务中高效运用这一数据结构解决问题。
1064 0
|
存储 JSON NoSQL
redis基本数据结构(String,Hash,Set,List,SortedSet)【学习笔记】
这篇文章是关于Redis基本数据结构的学习笔记,包括了String、Hash、Set、List和SortedSet的介绍和常用命令。文章解释了每种数据结构的特点和使用场景,并通过命令示例演示了如何在Redis中操作这些数据结构。此外,还提供了一些练习示例,帮助读者更好地理解和应用这些数据结构。
redis基本数据结构(String,Hash,Set,List,SortedSet)【学习笔记】
|
存储 消息中间件 NoSQL
Redis数据结构:List类型全面解析
Redis数据结构——List类型全面解析:存储多个有序的字符串,列表中每个字符串成为元素 Eelement,最多可以存储 2^32-1 个元素。可对列表两端插入(push)和弹出(pop)、获取指定范围的元素列表等,常见命令。 底层数据结构:3.2版本之前,底层采用**压缩链表ZipList**和**双向链表LinkedList**;3.2版本之后,底层数据结构为**快速链表QuickList** 列表是一种比较灵活的数据结构,可以充当栈、队列、阻塞队列,在实际开发中有很多应用场景。
1673 5
|
设计模式 安全 容器
数据结构第一篇【探究List和ArrayList之间的奥秘 】
数据结构第一篇【探究List和ArrayList之间的奥秘 】
293 5
|
存储 缓存 应用服务中间件
Nginx入门 -- 基本数据结构中之ngx_list_t,ngx_queue_t
Nginx入门 -- 基本数据结构中之ngx_list_t,ngx_queue_t
551 1
|
存储 缓存 Python
Python中的列表(List)和元组(Tuple)是两种重要的数据结构
【7月更文挑战第12天】Python中的列表(List)和元组(Tuple)是两种重要的数据结构
625 1
|
存储 NoSQL Redis
Redis入门到通关之Redis数据结构-List篇
Redis入门到通关之Redis数据结构-List篇
401 1
|
存储 索引 Python
Python中的基础数据结构:列表(List)详解
本文将深入探讨Python中的基础数据结构——列表(List),包括其创建、访问、修改、常用操作以及背后的原理。通过示例代码,帮助读者更好地理解和应用列表。

热门文章

最新文章