前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >专栏 >Spring入门:Resource

Spring入门:Resource

作者头像
WEBJ2EE
发布于 2019-08-14 12:25:22
发布于 2019-08-14 12:25:22
90200
代码可运行
举报
文章被收录于专栏:WebJ2EEWebJ2EE
运行总次数:0
代码可运行

1. Resource ?

Spring 把所有能记录信息的载体,如各种类型的文件、二进制流等都称为资源,对 Spring 开发者来说,最常用的资源就是 Spring 配置文件(通常是一份 XML 格式的文件)。 Spring 资源访问剖析和策略模式应用(李刚)

2. 为什么 Spring 要搞 Resource ?

一方面:增强 Java 原生的资源访问能力

Spring 需要与各式各样的资源打交道:

  • Url 资源(网络资源)
  • classpath 资源(类加载路径里的资源)
  • File 资源(文件系统
  • SerlvetContext 资源(相对于 ServletContext 路径里的资源)
  • 自定义资源(开发通过 ByteArray、InputStream 自由构造)

然而,用 Java 的 File、URL 访问这些底层资源的步骤过于繁琐。Spring 为资源访问提供了一个 Resource 接口,该接口提供了更强的资源访问能力。

图:Resource 接口

另一方面:对资源进行统一抽象,屏蔽资源访问细节

Resource 接口就是策略模式的典型应用,应用只和 Resource 接口耦合,并不知道底层采用何种资源访问策略,这样应用可以在不同的资源访问策略之间自由切换。

Spring 为 Resource 接口提供了如下实现类:

  • UrlResource:用于访问网络资源
  • ClassPathResource:用于访问类加载路径中资源
  • FileSystemResource:用于访问文件系统中资源
  • ServletContextResource:用于 ServletContext 路径中的资源
  • InputStreamResource:用于自定义资源来源
  • ByteArrayResource:用于自定义资源来源

3. Resource 应用示例

Resource 不仅可在 Spring 的项目中使用,也可直接作为资源访问的工具类使用。意思是说:即使不使用 Spring 框架,也可以使用 Resource 作为工具类,用来代替 URL。当然,使用 Resource 接口会让代码与 Spring 的接口耦合在一起,但这种耦合只是部分工具集的耦合,不会造成太大的代码污染。

示例1:FileSystemResource

代码语言:javascript
代码运行次数:0
运行
复制
import org.apache.commons.io.IOUtils;import org.springframework.core.io.FileSystemResource;
import java.io.IOException;import java.util.Date;
public class ResourceDemo {    public static void main(String[] args) throws IOException {        FileSystemResource fsr = new FileSystemResource("E:/springdemo/src/main/resources/webj2ee.txt");        System.out.println("exists: "+fsr.exists());        System.out.println("readable: "+fsr.isReadable());        System.out.println("writable: "+fsr.isWritable());        System.out.println("path: "+fsr.getPath());        System.out.println("fileName: "+fsr.getFilename());        System.out.println("lastModified: "+new Date(fsr.lastModified()));        System.out.println("description: "+fsr.getDescription());        System.out.println("content: "+IOUtils.toString(fsr.getInputStream(), "UTF-8"));    }}

示例2:ClassPathResource

代码语言:javascript
代码运行次数:0
运行
复制
import org.apache.commons.io.IOUtils;import org.springframework.core.io.ClassPathResource;
import java.io.IOException;import java.util.Date;
public class ResourceDemo {    public static void main(String[] args) throws IOException {
        ClassPathResource fsr = new ClassPathResource("webj2ee/webj2ee.txt");        System.out.println("exists: "+fsr.exists());        System.out.println("readable: "+fsr.isReadable());        System.out.println("isFile: "+fsr.isFile());        System.out.println("path: "+fsr.getPath());        System.out.println("fileName: "+fsr.getFilename());        System.out.println("lastModified: "+new Date(fsr.lastModified()));        System.out.println("description: "+fsr.getDescription());        System.out.println("content: "+IOUtils.toString(fsr.getInputStream(), "UTF-8"));    }}

示例3:ServletContextResource

代码语言:javascript
代码运行次数:0
运行
复制
package webj2ee;
import org.apache.commons.io.IOUtils;import org.springframework.web.context.support.ServletContextResource;
import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;import java.util.Date;
@WebServlet(name = "SCRDemo",urlPatterns = {"/SCRDemo"})public class SCRDemo extends HttpServlet {    @Override    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        ServletContextResource fsr = new ServletContextResource(request.getServletContext(), "webj2ee-servletcontext/webj2ee.txt");        System.out.println("exists: " + fsr.exists());        System.out.println("readable: " + fsr.isReadable());        System.out.println("path: " + fsr.getPath());        System.out.println("URL: "+fsr.getURL());        System.out.println("URI: "+fsr.getURI());        System.out.println("fileName: " + fsr.getFilename());        System.out.println("lastModified: " + new Date(fsr.lastModified()));        System.out.println("description: " + fsr.getDescription());        System.out.println("content: " + IOUtils.toString(fsr.getInputStream(), "UTF-8"));    }}

总结:Spring 提供的各种 Resource 实现类非常实用,避免了直接使用 URL、File 的繁琐、复杂,建议在项目中直接使用。

4. Resource 基本原理

Resource 的各实现类

涉及IO、NIO、JAR、File、Classpath 众多技术细节

留待后续文章详细分析

Resource 总体架构:

InputStreamSource.java 源码:

代码语言:javascript
代码运行次数:0
运行
复制
package org.springframework.core.io;
import java.io.IOException;import java.io.InputStream;
/** * Simple interface for objects that are sources for an {@link InputStream}. * * <p>This is the base interface for Spring's more extensive {@link Resource} interface. * * <p>For single-use streams, {@link InputStreamResource} can be used for any * given {@code InputStream}. Spring's {@link ByteArrayResource} or any * file-based {@code Resource} implementation can be used as a concrete * instance, allowing one to read the underlying content stream multiple times. * This makes this interface useful as an abstract content source for mail * attachments, for example. */public interface InputStreamSource {  /**   * Return an {@link InputStream} for the content of an underlying resource.   * <p>It is expected that each call creates a <i>fresh</i> stream.   * <p>This requirement is particularly important when you consider an API such   * as JavaMail, which needs to be able to read the stream multiple times when   * creating mail attachments. For such a use case, it is <i>required</i>   * that each {@code getInputStream()} call returns a fresh stream.   * @return the input stream for the underlying resource (must not be {@code null})   * @throws java.io.FileNotFoundException if the underlying resource doesn't exist   * @throws IOException if the content stream could not be opened   */  InputStream getInputStream() throws IOException;}

Resource.java 源码节选:

代码语言:javascript
代码运行次数:0
运行
复制
package org.springframework.core.io;
import java.io.File;import java.io.IOException;import java.io.InputStream;import java.net.URI;import java.net.URL;import java.nio.channels.Channels;import java.nio.channels.ReadableByteChannel;
import org.springframework.lang.Nullable;
/** * Interface for a resource descriptor that abstracts from the actual * type of underlying resource, such as a file or class path resource. * * <p>An InputStream can be opened for every resource if it exists in * physical form, but a URL or File handle can just be returned for * certain resources. The actual behavior is implementation-specific. */public interface Resource extends InputStreamSource {
  /**   * Determine whether this resource actually exists in physical form.   * <p>This method performs a definitive existence check, whereas the   * existence of a {@code Resource} handle only guarantees a valid   * descriptor handle.   */  boolean exists();
  /**   * Indicate whether non-empty contents of this resource can be read via   * {@link #getInputStream()}.   * <p>Will be {@code true} for typical resource descriptors that exist   * since it strictly implies {@link #exists()} semantics as of 5.1.   * Note that actual content reading may still fail when attempted.   * However, a value of {@code false} is a definitive indication   * that the resource content cannot be read.   * @see #getInputStream()   * @see #exists()   */  default boolean isReadable() {    return exists();  }
  /**   * Indicate whether this resource represents a handle with an open stream.   * If {@code true}, the InputStream cannot be read multiple times,   * and must be read and closed to avoid resource leaks.   * <p>Will be {@code false} for typical resource descriptors.   */  default boolean isOpen() {    return false;  }
  /**   * Determine whether this resource represents a file in a file system.   * A value of {@code true} strongly suggests (but does not guarantee)   * that a {@link #getFile()} call will succeed.   * <p>This is conservatively {@code false} by default.   * @since 5.0   * @see #getFile()   */  default boolean isFile() {    return false;  }
  /**   * Return a URL handle for this resource.   * @throws IOException if the resource cannot be resolved as URL,   * i.e. if the resource is not available as descriptor   */  URL getURL() throws IOException;
  /**   * Return a URI handle for this resource.   * @throws IOException if the resource cannot be resolved as URI,   * i.e. if the resource is not available as descriptor   * @since 2.5   */  URI getURI() throws IOException;
  /**   * Return a File handle for this resource.   * @throws java.io.FileNotFoundException if the resource cannot be resolved as   * absolute file path, i.e. if the resource is not available in a file system   * @throws IOException in case of general resolution/reading failures   * @see #getInputStream()   */  File getFile() throws IOException;  /**   * Determine the content length for this resource.   * @throws IOException if the resource cannot be resolved   * (in the file system or as some other known physical resource type)   */  long contentLength() throws IOException;
  /**   * Determine the last-modified timestamp for this resource.   * @throws IOException if the resource cannot be resolved   * (in the file system or as some other known physical resource type)   */  long lastModified() throws IOException;
  /**   * Create a resource relative to this resource.   * @param relativePath the relative path (relative to this resource)   * @return the resource handle for the relative resource   * @throws IOException if the relative resource cannot be determined   */  Resource createRelative(String relativePath) throws IOException;
  /**   * Determine a filename for this resource, i.e. typically the last   * part of the path: for example, "myfile.txt".   * <p>Returns {@code null} if this type of resource does not   * have a filename.   */  @Nullable  String getFilename();
  /**   * Return a description for this resource,   * to be used for error output when working with the resource.   * <p>Implementations are also encouraged to return this value   * from their {@code toString} method.   * @see Object#toString()   */  String getDescription();}

AbstractResource.java 源码节选:

代码语言:javascript
代码运行次数:0
运行
复制
package org.springframework.core.io;
import java.io.File;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.net.URI;import java.net.URISyntaxException;import java.net.URL;import java.nio.channels.Channels;import java.nio.channels.ReadableByteChannel;
import org.springframework.core.NestedIOException;import org.springframework.lang.Nullable;import org.springframework.util.ResourceUtils;
/** * Convenience base class for {@link Resource} implementations, * pre-implementing typical behavior. * * <p>The "exists" method will check whether a File or InputStream can * be opened; "isOpen" will always return false; "getURL" and "getFile" * throw an exception; and "toString" will return the description. * * @author Juergen Hoeller * @since 28.12.2003 */public abstract class AbstractResource implements Resource {
  /**   * This implementation checks whether a File can be opened,   * falling back to whether an InputStream can be opened.   * This will cover both directories and content resources.   */  @Override  public boolean exists() {    // Try file existence: can we find the file in the file system?    try {      return getFile().exists();    }    catch (IOException ex) {      // Fall back to stream existence: can we open the stream?      try {        getInputStream().close();        return true;      }      catch (Throwable isEx) {        return false;      }    }  }
  /**   * This implementation always returns {@code true} for a resource   * that {@link #exists() exists} (revised as of 5.1).   */  @Override  public boolean isReadable() {    return exists();  }
  /**   * This implementation always returns {@code false}.   */  @Override  public boolean isOpen() {    return false;  }
  /**   * This implementation always returns {@code false}.   */  @Override  public boolean isFile() {    return false;  }
  /**   * This implementation throws a FileNotFoundException, assuming   * that the resource cannot be resolved to a URL.   */  @Override  public URL getURL() throws IOException {    throw new FileNotFoundException(getDescription() + " cannot be resolved to URL");  }
  /**   * This implementation builds a URI based on the URL returned   * by {@link #getURL()}.   */  @Override  public URI getURI() throws IOException {    URL url = getURL();    try {      return ResourceUtils.toURI(url);    }    catch (URISyntaxException ex) {      throw new NestedIOException("Invalid URI [" + url + "]", ex);    }  }
  /**   * This implementation throws a FileNotFoundException, assuming   * that the resource cannot be resolved to an absolute file path.   */  @Override  public File getFile() throws IOException {    throw new FileNotFoundException(getDescription() + " cannot be resolved to absolute file path");  }
  /**   * This implementation returns {@link Channels#newChannel(InputStream)}   * with the result of {@link #getInputStream()}.   * <p>This is the same as in {@link Resource}'s corresponding default method   * but mirrored here for efficient JVM-level dispatching in a class hierarchy.   */  @Override  public ReadableByteChannel readableChannel() throws IOException {    return Channels.newChannel(getInputStream());  }
  /**   * This implementation reads the entire InputStream to calculate the   * content length. Subclasses will almost always be able to provide   * a more optimal version of this, e.g. checking a File length.   * @see #getInputStream()   */  @Override  public long contentLength() throws IOException {    InputStream is = getInputStream();    try {      long size = 0;      byte[] buf = new byte[256];      int read;      while ((read = is.read(buf)) != -1) {        size += read;      }      return size;    }    finally {      try {        is.close();      }      catch (IOException ex) {      }    }  }
  /**   * This implementation checks the timestamp of the underlying File,   * if available.   * @see #getFileForLastModifiedCheck()   */  @Override  public long lastModified() throws IOException {    File fileToCheck = getFileForLastModifiedCheck();    long lastModified = fileToCheck.lastModified();    if (lastModified == 0L && !fileToCheck.exists()) {      throw new FileNotFoundException(getDescription() +          " cannot be resolved in the file system for checking its last-modified timestamp");    }    return lastModified;  }
  /**   * Determine the File to use for timestamp checking.   * <p>The default implementation delegates to {@link #getFile()}.   * @return the File to use for timestamp checking (never {@code null})   * @throws FileNotFoundException if the resource cannot be resolved as   * an absolute file path, i.e. is not available in a file system   * @throws IOException in case of general resolution/reading failures   */  protected File getFileForLastModifiedCheck() throws IOException {    return getFile();  }
  /**   * This implementation throws a FileNotFoundException, assuming   * that relative resources cannot be created for this resource.   */  @Override  public Resource createRelative(String relativePath) throws IOException {    throw new FileNotFoundException("Cannot create a relative resource for " + getDescription());  }
  /**   * This implementation always returns {@code null},   * assuming that this resource type does not have a filename.   */  @Override  @Nullable  public String getFilename() {    return null;  }

  /**   * This implementation compares description strings.   * @see #getDescription()   */  @Override  public boolean equals(Object other) {    return (this == other || (other instanceof Resource &&        ((Resource) other).getDescription().equals(getDescription())));  }
  /**   * This implementation returns the description's hash code.   * @see #getDescription()   */  @Override  public int hashCode() {    return getDescription().hashCode();  }
  /**   * This implementation returns the description of this resource.   * @see #getDescription()   */  @Override  public String toString() {    return getDescription();  }}

参考:

https://docs.spring.io/spring-framework/docs/current/spring-framework-reference/core.html#resources https://www.ibm.com/developerworks/cn/java/j-lo-spring-resource/index.html https://tools.ietf.org/html/rfc3986

本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2019-08-11,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 WebJ2EE 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
JDK1.9-模拟斗地主洗牌发牌
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
cwl_java
2019/12/03
7360
iOS 斗地主发牌排序
很多人都玩过斗地主,也有很多人没玩过,或者像我一样是个菜B,不太懂怎么玩,好,没关系,这篇文章不是教你斗地主,是要根据斗地主这个游戏做些技术分享: 目的:随机发牌,发的牌按牌大小排序(花色与数字)
清墨
2018/05/07
1.3K0
iOS 斗地主发牌排序
扑克牌例题与Collections工具类
我们需要创建四个类,一个封装对象的属性,一个封装牌的花色和大小也就是牌的类型,一个实现发牌,排序,洗牌功能,也就是封装对象的行为,最后一个实现图形化界面。
端碗吹水
2020/09/23
3200
这是一道算法题:如何比较徳州扑克牌大小?(附带python实现)
每个玩家有2张牌,公共牌有5张牌,共计7张牌。比牌时,每个玩家要找到自己组成最大牌型的5张,跟其他玩家的最大牌型比大小。
HullQin
2023/07/26
3K0
用扑克牌演示 Python 数据分析
扑克牌是我们常见一种娱乐工具,玩法千变万化,为了提高学习 Python 知识的趣味性,我构建了一个扑克牌的数据框,将用它来演示一些 Python 数据分析的功能。
张俊红
2019/09/30
1.4K0
【Java】Java基础 使用集合实现斗地主分牌
我们到时候分的牌都存储在这里,但你可能会有疑问,因为存储的泛型是Integer,但扑克牌是有花色的,这该如何实现?
哈__
2024/04/25
1400
【Java】Java基础 使用集合实现斗地主分牌
P2668 斗地主 贪心+深搜
题目描述 牛牛最近迷上了一种叫斗地主的扑克游戏。斗地主是一种使用黑桃、红心、梅花、方片的A到K加上大小王的共54张牌来进行的扑克牌游戏。在斗地主中,牌的大小关系根据牌的数码表示如下:3<4<5<6<7<8<9<10<J<Q<K<A<2<小王<大王,而花色并不对牌的大小产生影响。每一局游戏中,一副手牌由n张牌组成。游戏者每次可以根据规定的牌型进行出牌,首先打光自己的手牌一方取得游戏的胜利。 现在,牛牛只想知道,对于自己的若干组手牌,分别最少需要多少次出牌可以将它们打光。请你帮他解决这个问题。 需要注意的是,本
attack
2018/04/13
2K0
P2668 斗地主 贪心+深搜
2106. [NOIP2015] 斗地主
2106. [NOIP2015] 斗地主 ★★★☆   输入文件:landlords.in   输出文件:landlords.out 简单对比 时间限制:2 s   内存限制:1025 MB 【题目描述】 牛牛最近迷上了一种叫斗地主的扑克游戏。斗地主是一种使用黑桃、红心、梅花、方片的A到K加上大小王的共54张牌来进行的扑克牌游戏。在斗地主中,牌的大小关系根据牌的数码表示如下:3<4<5<6<7<8<9<10<J<Q<K<A<2<小王<大王,而花色并不对牌的大小产生影响。每一局游戏中,一副手牌由
attack
2018/04/13
1.6K0
【剑指Offer】61.扑克牌中的顺子
LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张_)…他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子…LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张牌就可以变成“1,2,3,4,5”(大小王分别看作2和4),“So Lucky!”。LL决定去买体育彩票啦。 现在,要求你使用这幅牌模拟上面的过程,然后告诉我们LL的运气如何, 如果牌能组成顺子就输出true,否则就输出false。为了方便起见,你可以认为大小王是0。
瑞新
2020/12/07
2810
剑指Offer-扑克牌顺子
题目描述 LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子.....LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张牌就可以变成“1,2,3,4,5”(大小王分别看作2和4),“So Lucky!”。LL决定去买
武培轩
2018/04/19
5900
【心碎篇】冲刺CSP-J/S第2轮倒计时5天:斗地主,栽了
这道题:斗地主,在小码匠让我给她准备食材的时候就开始搞,下午继续搞了2.5个小时,最终也没搞定。
小码匠
2023/10/24
2900
【心碎篇】冲刺CSP-J/S第2轮倒计时5天:斗地主,栽了
剑指offer 扑克牌顺子
LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)…他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子…..LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张牌就可以变成“1,2,3,4,5”(大小王分别看作2和4),“So Lucky!”。LL决定去买体育彩票啦。 现在,要求你使用这幅牌模拟上面的过程,然后告诉我们LL的运气如何。为了方便起见,你可以认为大小王是0。
vincentbbli
2021/08/18
2270
剑指offer——扑克牌顺子
题目描述 LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)…他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子…..LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张牌就可以变成“1,2,3,4,5”(大小王分别看作2和4),“So Lucky!”。LL决定去买体育彩票啦。 现在,要求你使用这幅牌模拟上面的过程,然后告诉我们LL的运气如何, 如果牌能组成顺子就输出true,否则就输出false。为了方便起见,你可以认为大小王是0。
AI那点小事
2020/04/18
2870
如何优雅地给扑克牌排序?(一)——排序算法的数学本质
不知平常各位打牌时候是否遇到过这样的场景:四人打完升级后,面对两幅混乱的扑克牌,走了一人后想打斗地主,现在要把他们分出一副来,于是打算先排序后分离,然后各种花色,数字,摆满一桌子,乱成一团,等排好了,5分钟过去了……
magic2728
2019/09/27
2K0
如何优雅地给扑克牌排序?(一)——排序算法的数学本质
剑指offer 扑克牌顺子
LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)...他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子.....LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张牌就可以变成“1,2,3,4,5”(大小王分别看作2和4),“So Lucky!”。LL决定去买体育彩票啦。 现在,要求你使用这幅牌模拟上面的过程,然后告诉我们LL的运气如何, 如果牌能组成顺子就输出true,否则就输出false。为了方便起见,你可以认为大小王是0。
week
2019/03/29
3080
每天一道剑指offer-扑克牌顺子
考试结束,班级平均分只拿到了年级第二,班主任于是问道:大家都知道世界第一高峰珠穆朗玛峰,有人知道世界第二高峰是什么吗?正当班主任要继续发话,只听到角落默默响起来一个声音:”乔戈里峰” 题目 每天一道剑
乔戈里
2019/01/28
4850
用斗地主的实例学会使用java Collections工具类
最近在学习数据结构和算法的过程中频繁用到了Collections工具类,这是开发中的一把利器,简化了许多涉及集合的编码,该文将通过实例对此工具类进入深入剖析。
智慧zhuhuix
2020/08/14
7051
用斗地主的实例学会使用java Collections工具类
斗地主之洗牌发牌----Java篇
区别: 上面是用一个字符串数组来存储所有牌的组合,并且也是对字符串数组进行洗牌操作,较为麻烦,而用哈希表之后,我们可以对每一张牌对应的索引进行洗牌操作。
大忽悠爱学习
2021/11/15
8560
扑克牌大小
题目描述 扑克牌游戏大家应该都比较熟悉了,一副牌由54张组成,含3~A,2各4张,小王1张,大王1张。牌面从小到大用如下字符和字符串表示(其中,小写joker表示小王,大写JOKER表示大王):) 3 4 5 6 7 8 9 10 J Q K A 2 joker JOKER 输入两手牌,两手牌之间用“-”连接,每手牌的每张牌以空格分隔,“-”两边没有空格,如:4 4 4 4-joker JOKER 请比较两手牌大小,输出较大的牌,如果不存在比较关系则输出ERROR
AI那点小事
2020/04/20
7450
[剑指offer] 扑克牌顺子
LL今天心情特别好,因为他去买了一副扑克牌,发现里面居然有2个大王,2个小王(一副牌原本是54张^_^)…他随机从中抽出了5张牌,想测测自己的手气,看看能不能抽到顺子,如果抽到的话,他决定去买体育彩票,嘿嘿!!“红心A,黑桃3,小王,大王,方片5”,“Oh My God!”不是顺子…..LL不高兴了,他想了想,决定大\小 王可以看成任何数字,并且A看作1,J为11,Q为12,K为13。上面的5张牌就可以变成“1,2,3,4,5”(大小王分别看作2和4),“So Lucky!”。LL决定去买体育彩票啦。 现在,要求你使用这幅牌模拟上面的过程,然后告诉我们LL的运气如何, 如果牌能组成顺子就输出true,否则就输出false。为了方便起见,你可以认为大小王是0。
尾尾部落
2018/09/04
7170
相关推荐
JDK1.9-模拟斗地主洗牌发牌
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档