目录
🚆这篇文章是对 ArrayList 中常用的方法进行练习~
🚀关于 ArrayList 的内容总结:【Java---数据结构】ArrayList
一、删除第一个字符串中出现的第二个字符串中的字符。
📌题目示例:
String str1 = "welcome to csdn";
String str2 = "come";
输出结果:wl t sdn
🌊代码示例:
import java.util.ArrayList;
public class Test {
public static void main(String[] args) {
String str1 = "welcome to csdn";
String str2 = "come";
ArrayList<String> strings = new ArrayList<>();
for (int i=0;i<str1.length();i++) {
char ch = str1.charAt(i);
if (!str2.contains(ch + "")) {
strings.add(ch + "");
}
}
for (String str:strings) {
System.out.print(str);
}
}
}
//运行结果:wl t sdn
二、扑克牌练习
- 一张扑克牌由数字和花色构成。
- 一张牌就是一个对象,将一张牌放到一个类中
class Card{
private int rank;//数字
private String suit; //花色
public Card(int rank, String suit) {
this.rank = rank;
this.suit = suit;
}
@Override
public String toString() {
return "[ " + this.suit+" " + this.rank + " ]";
}
}
主要实现的功能:
- 构造一副牌(买牌):没有大小王,1,2,3,4,……10,11,12,13
//存放牌的花色
private static final String[] suits = {"♥","♠","♣","♦"};
//买牌
public static List<Card> buyCard(){
ArrayList<Card> cards = new ArrayList<>();
for (int i = 0; i < 4; i++) {
for (int j = 1; j <=13 ; j++) {
/* String suit = suits[i];
int rank = j;
Card card = new Card(j,suit[i]);
cards.add(card);*/
cards.add(new Card(j,suits[i]));
}
}
return cards;
}
洗牌:
- 写一个方法交换两个牌,使用 Random 生成随机数,可以随即交换两张牌,最终将牌打乱。
//交换牌
private 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 shuttle(List<Card> cards){
for (int i = cards.size()-1; i > 0 ; i--) {
Random random = new Random();
int ran = random.nextInt(i);
swap(cards,i,ran);
}
}
揭牌:
- 3个人,每人轮流揭5张牌。
System.out.println("揭牌:3个人没人轮流揭5张牌");
ArrayList<List<Card>> hand = new ArrayList<>(); //二维数组
List<Card> hand1 = new ArrayList<>();
List<Card> hand2 = new ArrayList<>();
List<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++) {
Card card = cards.remove(0);
hand.get(j).add(card);
}
}
完整代码:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
class Card{
private int rank;//数字
private String suit; //花色
public Card(int rank, String suit) {
this.rank = rank;
this.suit = suit;
}
@Override
public String toString() {
return "[ " + this.suit+" " + this.rank + " ]";
}
}
//没有大小王,1,2,3,4,……10,11,12,13
public class TestDemo2 {
//存放牌的花色
private static final String[] suits = {"♥","♠","♣","♦"};
//买牌
public static List<Card> buyCard(){
ArrayList<Card> cards = new ArrayList<>();
for (int i = 0; i < 4; i++) {
for (int j = 1; j <=13 ; j++) {
/* String suit = suits[i];
int rank = j;
Card card = new Card(j,suit[i]);
cards.add(card);*/
cards.add(new Card(j,suits[i]));
}
}
return cards;
}
//交换牌
private 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 shuttle(List<Card> cards){
for (int i = cards.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);
shuttle(cards);
System.out.println("洗牌:"+cards);
System.out.println("揭牌:3个人没人轮流揭5张牌");
ArrayList<List<Card>> hand = new ArrayList<>(); //二维数组
List<Card> hand1 = new ArrayList<>();
List<Card> hand2 = new ArrayList<>();
List<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++) {
Card card = cards.remove(0);
hand.get(j).add(card);
}
}
System.out.println("1手中的牌:"+hand1);
System.out.println("2手中的牌:"+hand2);
System.out.println("3手中的牌:"+hand3);
System.out.println("剩下的牌:"+cards);
}
}
三、杨辉三角
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> ret = new ArrayList<>();
//第一行
List<Integer> list1 = new ArrayList<>();
list1.add(1); //存储第一行的数据
ret.add(list1);
for(int i=1;i<numRows;i++){
List<Integer> list = new ArrayList<>();
list.add(1);//每一行的开始都是1
List<Integer> preRow = ret.get(i-1);
for(int j=1;j<i;j++){
//中间情况
int num1 = preRow.get(j)+preRow.get(j-1);
list.add(num1);
}
list.add(1);//每一行的最后都是1
ret.add(list);
}
return ret;
}
}