首页 文章 精选 留言 我的

精选列表

搜索[java ee],共10000篇文章
优秀的个人博客,低调大师

让你一看就懂的快速排序算法(Java)

快速排序 你也许会被快速排序的文章弄得迷迷糊糊,其实大体上去看,快速排序就一步:找个数做基准数,把小于它的数移到它左边,把大于它的数移到它右边。这句话是核心。然后我们只需要让基准数左边的重复上面的步骤,让基准数右边的再重复上面的步骤就完了。 比如我们有一个数组: int[] nums = {5, 2, 6, 8, 4, 7, 9, 1}; 快速排序的思想就是使用递归,其实使用递归并不是多么复杂,在理解算法的思想后,只需要关注算法中重复的步骤,那就是递归的核心代码。 比如快速排序的算法思想,大体代码如下: public void quick(int left, int right) { /* * 给这段代码起个名字为:基准校验 * 把小于基准数的移到左边,把大于基准数的移到右边 */ quick(left, now); //继续处理左边的 quick(now, right); //继续处理右边的 } 经过一遍基准校验,我们就找到了该基准数在完全排序后的正确位置! 大体算法的流程图: 写出上面的大体算法步骤,就表示我们已经有了雏形,现在该如何实现找个数做基准数,把小于它的数移到它左边,把大于它的数移到它右边呢? 排除不满足的情况: //已经不满足条件就代表可以不用递归了 if (left > right) { return; } 我们可以定义两个指针,用于遍历数组: int start = left;//起点下标 int end = right;//终点下标 再找一个基准数: int temp = nums[left];//把第一个数作为基准点 遍历代码: /*如果左右指针还没有走到一起,代表还有位置没有遍历*/ while (start != end) { //右指针先走,找到小于基准数的停止 while (start < end && nums[end] >= temp) { end--; //这是往左移动指针 } //左指针后走,找到大于基准数的停止 while (start < end && nums[start] <= temp) { start++; //这是往右移动指针 } //如果左右指针在未相遇时都找到了目标,则交换位置 if (start < end) { int i = nums[start]; nums[start] = nums[end]; nums[end] = i; } //左右指针走到一起,则遍历结束 } //把基准数与该点交换位置,因为这就是基准数的正确位置 nums[left] = nums[start]; nums[start] = temp; 如果还是不太清楚,不如自己动手试试! 拷贝完整代码: //定义一个数组 static int[] nums = {5, 2, 6, 8, 4, 7, 9, 1}; static int n = nums.length - 1; /** * 递归的数据结构就是栈 * left right代表该段数组的起点和终点 */ public void quick(int left, int right) { //已经不满足条件就可以不用递归了 if (left > right) { return; } //定义俩指针 用于移动 int start = left;//起点下标 int end = right;//终点下标 int temp = nums[left];//把第一个数作为基准点 pri(left, right);//打印此时的结果,不用在意 while (start != end) { //如果左右指针还没有走到一起,代表还有位置没有遍历 while (start < end && nums[end] >= temp) { //右指针先走,找到小于基准数的停止 end--; //这是往左在移动指针 } while (start < end && nums[start] <= temp) { //左指针后走,找到大于基准数的停止 start++; //这是往右在移动指针 } if (start < end) { //如果左右指针在未相遇时都找到了目标,则交换位置 int i = nums[start]; nums[start] = nums[end]; nums[end] = i; } } //此时的left和right走到了一起 //把基准数与该点交换位置 nums[left] = nums[start]; nums[start] = temp; prin(start);//打印输出,不用在意 //以上代码的作用就是把小于基准数的移到左边,把大于基准数的移到右边 quick(left, start - 1); //继续处理左边的,这里是一个递归的过程 quick(start + 1, right); //继续处理右边的 ,这里是一个递归的过程 } /** * 主程序入口 */ public static void main(String[] args) { new Test().quick(0, n); } /** * 以下代码忽略即可,用于打印输出 */ private void pri(int start, int end) { StringBuffer s = new StringBuffer(); s.append("对数组 ["); while (start <= end) { s.append(nums[start] + " "); start++; } s.append("]"); s.append(" 排序"); System.out.print(s); } private void prin(int j) { StringBuffer s = new StringBuffer(); s.append(", 排序后 ["); int start = 0; while (start <= n) { if (start == j) { s.append("(" + nums[start] + ") "); } else { s.append(nums[start] + " "); } start++; } s.append("]"); s.append(" "); System.out.println(s); } 打印输出: 对数组 [5 2 6 8 4 7 9 1 ] 排序, 排序后 [4 2 1 (5) 8 7 9 6 ] 对数组 [4 2 1 ] 排序, 排序后 [1 2 (4) 5 8 7 9 6 ] 对数组 [1 2 ] 排序, 排序后 [(1) 2 4 5 8 7 9 6 ] 对数组 [2 ] 排序, 排序后 [1 (2) 4 5 8 7 9 6 ] 对数组 [8 7 9 6 ] 排序, 排序后 [1 2 4 5 6 7 (8) 9 ] 对数组 [6 7 ] 排序, 排序后 [1 2 4 5 (6) 7 8 9 ] 对数组 [7 ] 排序, 排序后 [1 2 4 5 6 (7) 8 9 ] 对数组 [9 ] 排序, 排序后 [1 2 4 5 6 7 8 (9) ]

优秀的个人博客,低调大师

JAVA实现QRCode的二维码生成以及打印

喜欢的朋友可以关注下,粉丝也缺。 不说废话了直接上代码 注意使用QRCode是需要zxing的核心jar包,这里给大家提供下载地址 https://download.csdn.net/download/dsn727455218/10515340 1.二维码的工具类 public class QR_Code { private static int BLACK = 0x000000; private static int WHITE = 0xFFFFFF; /** * 1.创建最原始的二维码图片 * * @param info * @return */ private BufferedImage createCodeImage(CodeModel info) { String contents = info.getContents();//获取正文 int width = info.getWidth();//宽度 int height = info.getHeight();//高度 Map<EncodeHintType, Object> hint = new HashMap<>(); hint.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);//设置二维码的纠错级别【级别分别为M L H Q ,H纠错能力级别最高,约可纠错30%的数据码字】 hint.put(EncodeHintType.CHARACTER_SET, info.getCharacter_set());//设置二维码编码方式【UTF-8】 hint.put(EncodeHintType.MARGIN, 0); MultiFormatWriter writer = new MultiFormatWriter(); BufferedImage img = null; try { //构建二维码图片 //QR_CODE 一种矩阵二维码 BitMatrix bm = writer.encode(contents, BarcodeFormat.QR_CODE, width, height + 5, hint); int[] locationTopLeft = bm.getTopLeftOnBit(); int[] locationBottomRight = bm.getBottomRightOnBit(); info.setBottomStart(new int[] { locationTopLeft[0], locationBottomRight[1] }); info.setBottomEnd(locationBottomRight); int w = bm.getWidth(); int h = bm.getHeight(); img = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB); for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { img.setRGB(x, y, bm.get(x, y) ? BLACK : WHITE); } } } catch (WriterException e) { e.printStackTrace(); } return img; } /** * 2.为二维码增加logo和二维码下文字 logo--可以为null 文字--可以为null或者空字符串"" * * @param info * @param output */ private void dealLogoAndDesc(CodeModel info, OutputStream output) { //获取原始二维码图片 BufferedImage bm = createCodeImage(info); //获取Logo图片 File logoFile = info.getLogoFile(); int width = bm.getWidth(); int height = bm.getHeight(); Graphics g = bm.getGraphics(); //处理logo if (logoFile != null && logoFile.exists()) { try { BufferedImage logoImg = ImageIO.read(logoFile); int logoWidth = logoImg.getWidth(); int logoHeight = logoImg.getHeight(); float ratio = info.getLogoRatio();//获取Logo所占二维码比例大小 if (ratio > 0) { logoWidth = logoWidth > width * ratio ? (int) (width * ratio) : logoWidth; logoHeight = logoHeight > height * ratio ? (int) (height * ratio) : logoHeight; } int x = (width - logoWidth) / 2; int y = (height - logoHeight) / 2; //根据logo 起始位置 和 宽高 在二维码图片上画出logo g.drawImage(logoImg, x, y, logoWidth, logoHeight, null); } catch (Exception e) { e.printStackTrace(); } } //处理二维码下文字 String desc = info.getDesc(); if (!"".equals(desc)) { try { //设置文字字体 int whiteWidth = info.getHeight() - info.getBottomEnd()[1]; Font font = new Font("宋体", Font.PLAIN, info.getFontSize()); int fontHeight = g.getFontMetrics(font).getHeight(); //计算需要多少行 int lineNum = 1; int currentLineLen = 0; for (int i = 0; i < desc.length(); i++) { char c = desc.charAt(i); int charWidth = g.getFontMetrics(font).charWidth(c); if (currentLineLen + charWidth > width) { lineNum++; currentLineLen = 0; continue; } currentLineLen += charWidth; } int totalFontHeight = fontHeight * lineNum; int wordTopMargin = 4; BufferedImage bm1 = new BufferedImage(width, height + totalFontHeight + wordTopMargin - whiteWidth, BufferedImage.TYPE_INT_RGB); Graphics g1 = bm1.getGraphics(); if (totalFontHeight + wordTopMargin - whiteWidth > 0) { g1.setColor(Color.WHITE); g1.fillRect(0, height, width, totalFontHeight + wordTopMargin - whiteWidth); } g1.setColor(new Color(BLACK)); g1.setFont(font); int startX = (78 - (12 * desc.length())) / 2; g1.drawImage(bm, 0, 0, null); width = info.getBottomEnd()[0] - info.getBottomStart()[0]; height = info.getBottomEnd()[1] + 1; currentLineLen = 0; int currentLineIndex = 0; int baseLo = g1.getFontMetrics().getAscent(); for (int i = 0; i < desc.length(); i++) { String c = desc.substring(i, i + 1); int charWidth = g.getFontMetrics(font).stringWidth(c); if (currentLineLen + charWidth > width) { currentLineIndex++; currentLineLen = 0; g1.drawString(c, currentLineLen + startX - 5, -5 + height + baseLo + fontHeight * (currentLineIndex) + wordTopMargin); currentLineLen = charWidth; continue; } g1.drawString(c, currentLineLen + startX - 5, -5 + height + baseLo + fontHeight * (currentLineIndex) + wordTopMargin); currentLineLen += charWidth; } //处理二维码下日期 String date = info.getDate(); g1.drawString(date, 5, 6 + height + baseLo + fontHeight * (currentLineIndex) + wordTopMargin); g1.dispose(); bm = bm1; } catch (Exception e) { e.printStackTrace(); } } try { ImageIO.write(bm, info.getFormat(), output); } catch (Exception e) { e.printStackTrace(); } } /** * 3.创建 带logo和文字的二维码 * * @param info * @param file */ public void createCodeImage(CodeModel info, File file) { File parent = file.getParentFile(); if (!parent.exists()) parent.mkdirs(); OutputStream output = null; try { output = new BufferedOutputStream(new FileOutputStream(file)); dealLogoAndDesc(info, output); output.flush(); } catch (Exception e) { e.printStackTrace(); } finally { try { output.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * 3.创建 带logo和文字的二维码 * * @param info * @param filePath */ public void createCodeImage(CodeModel info, String filePath) { createCodeImage(info, new File(filePath)); } /** * 4.创建 带logo和文字的二维码 * * @param filePath */ public void createCodeImage(String contents, String filePath) { CodeModel codeModel = new CodeModel(); codeModel.setContents(contents); createCodeImage(codeModel, new File(filePath)); } /** * 5.读取 二维码 获取二维码中正文 * * @param input * @return */ public String decode(InputStream input) { Map<DecodeHintType, Object> hint = new HashMap<DecodeHintType, Object>(); hint.put(DecodeHintType.POSSIBLE_FORMATS, BarcodeFormat.QR_CODE); String result = ""; try { BufferedImage img = ImageIO.read(input); int[] pixels = img.getRGB(0, 0, img.getWidth(), img.getHeight(), null, 0, img.getWidth()); LuminanceSource source = new RGBLuminanceSource(img.getWidth(), img.getHeight(), pixels); BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source)); QRCodeReader reader = new QRCodeReader(); Result r = reader.decode(bitmap, hint); result = r.getText(); } catch (Exception e) { result = "读取错误"; } return result; } public static void main(String[] args) { String imgname = String.valueOf(System.currentTimeMillis()) + ".png"; CodeModel info = new CodeModel(); info.setContents("客户:倍特 品牌:倍特 型号:XH001 日期:2018-06-19 检验员:易工"); info.setWidth(68); info.setHeight(68); info.setFontSize(12); info.setLogoFile(new File("F:\\软件安全下载目录\\personnelManage\\" + imgname)); info.setDesc("玫瑰之约"); info.setDate("2018-06-19"); info.setLogoFile(null); QR_Code code = new QR_Code(); } } 2.二维码实体类 public class CodeModel { /** * @return the date */ public String getDate() { return date; } /** * @param date * the date to set */ public void setDate(String date) { this.date = date; } /** * @return the contents */ public String getContents() { return contents; } /** * @param contents * the contents to set */ public void setContents(String contents) { this.contents = contents; } /** * @return the width */ public int getWidth() { return width; } /** * @param width * the width to set */ public void setWidth(int width) { this.width = width; } /** * @return the height */ public int getHeight() { return height; } /** * @param height * the height to set */ public void setHeight(int height) { this.height = height; } /** * @return the format */ public String getFormat() { return format; } /** * @param format * the format to set */ public void setFormat(String format) { this.format = format; } /** * @return the character_set */ public String getCharacter_set() { return character_set; } /** * @param character_set * the character_set to set */ public void setCharacter_set(String character_set) { this.character_set = character_set; } /** * @return the fontSize */ public int getFontSize() { return fontSize; } /** * @param fontSize * the fontSize to set */ public void setFontSize(int fontSize) { this.fontSize = fontSize; } /** * @return the logoFile */ public File getLogoFile() { return logoFile; } /** * @param logoFile * the logoFile to set */ public void setLogoFile(File logoFile) { this.logoFile = logoFile; } /** * @return the logoRatio */ public float getLogoRatio() { return logoRatio; } /** * @param logoRatio * the logoRatio to set */ public void setLogoRatio(float logoRatio) { this.logoRatio = logoRatio; } /** * @return the desc */ public String getDesc() { return desc; } /** * @param desc * the desc to set */ public void setDesc(String desc) { this.desc = desc; } /** * @return the whiteWidth */ public int getWhiteWidth() { return whiteWidth; } /** * @param whiteWidth * the whiteWidth to set */ public void setWhiteWidth(int whiteWidth) { this.whiteWidth = whiteWidth; } /** * @return the bottomStart */ public int[] getBottomStart() { return bottomStart; } /** * @param bottomStart * the bottomStart to set */ public void setBottomStart(int[] bottomStart) { this.bottomStart = bottomStart; } /** * @return the bottomEnd */ public int[] getBottomEnd() { return bottomEnd; } /** * @param bottomEnd * the bottomEnd to set */ public void setBottomEnd(int[] bottomEnd) { this.bottomEnd = bottomEnd; } /** * 正文 */ private String contents; /** * 二维码宽度 */ private int width = 400; /** * 二维码高度 */ private int height = 400; /** * 图片格式 */ private String format = "png"; /** * 编码方式 */ private String character_set = "utf-8"; /** * 字体大小 */ private int fontSize = 12; /** * logo */ private File logoFile; /** * logo所占二维码比例 */ private float logoRatio = 0.20f; /** * 二维码下文字 */ private String desc; /** * 下方日期 */ private String date; private int whiteWidth;//白边的宽度 private int[] bottomStart;//二维码最下边的开始坐标 private int[] bottomEnd;//二维码最下边的结束坐标 } 3.action中调用 @ResponseBody @RequestMapping(value = "/addqrcode") public String addqrcode(HttpServletRequest request, String msg) throws Exception { String realPath = request.getSession().getServletContext().getRealPath("/") + "\\qrcode\\"; msg = String.valueOf(System.currentTimeMillis()) + ".png"; CodeModel info = new CodeModel(); info.setContents("客户:" + request.getParameter("customer") + " 品牌:" + request.getParameter("brand") + " 型号:" + request.getParameter("model") + " 日期:" + request.getParameter("addtime") + " 检验员:" + request.getParameter("testpersion")); info.setWidth(68); info.setHeight(68); info.setFontSize(12); info.setLogoFile(new File(realPath + msg)); info.setDesc(request.getParameter("brand")); info.setDate(request.getParameter("addtime")); info.setLogoFile(null); QR_Code code = new QR_Code(); code.createCodeImage(info, realPath + msg); return msg; } 以上方法就可以实现带logo,以及下方显示文字的二维码。不需要logo,参数可以传null。 接来下说说二维码的打印,根据实际需要可以自定义设置二维码的尺寸,以及图片的格式 4.打印方法 需要注意的是:一是生成保存二维码的地址,二是打印机驱动必须启动 /** * 打印二维码 * * @param fileName * @param count */ @ResponseBody @RequestMapping(value = "/printImage") public static void printImage(String fileName, int count, HttpServletRequest request) { String realPath = request.getSession().getServletContext().getRealPath("/") + "\\qrcode\\"; try { DocFlavor dof = null; if (fileName.endsWith(".gif")) { dof = DocFlavor.INPUT_STREAM.GIF; } else if (fileName.endsWith(".jpg")) { dof = DocFlavor.INPUT_STREAM.JPEG; } else if (fileName.endsWith(".png")) { dof = DocFlavor.INPUT_STREAM.PNG; } // 获取默认打印机 PrintService ps = PrintServiceLookup.lookupDefaultPrintService(); PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); // pras.add(OrientationRequested.PORTRAIT); // pras.add(PrintQuality.HIGH); pras.add(new Copies(count)); pras.add(MediaSizeName.ISO_A10); // 设置打印的纸张 DocAttributeSet das = new HashDocAttributeSet(); das.add(new MediaPrintableArea(0, 0, 1, 1, MediaPrintableArea.INCH)); FileInputStream fin = new FileInputStream(realPath + fileName); Doc doc = new SimpleDoc(fin, dof, das); DocPrintJob job = ps.createPrintJob(); job.print(doc, pras); fin.close(); } catch (IOException ie) { ie.printStackTrace(); } catch (PrintException pe) { pe.printStackTrace(); } } 看着是不是很简单,完美的解决。 到这里已经完成了对文件的预览功能,如有需要可以加我Q群【308742428】大家一起讨论技术。 后面会不定时为大家更新文章,敬请期待。 喜欢的朋友可以关注下,粉丝也缺。

优秀的个人博客,低调大师

Java中++,--,前缀后缀表达值的不同,与^的值计算

package 习题1; /** * ++ -- 与 ^ 的一些计算 * @author SeeClanUkyo * */ public class Test4 { public static void main(String[] args) { int i = 3; int b = (i++) + (--i) - (++i) + (i++); /** * 第一个i++为3时进行+计算,而后变为4, 第二个--i是将4递减到3, 第三个++i,3递增到了4, 第四个i++在4时进行计算,其后变为5. * * 所以3+3-4+4=6 */ System.out.println(b); ////////////////////////////// int j = 6; int k = (j++) - (--i) + (i++) - (--j); /** * 第一个j为6时进行计算,其后变为7 第二个i在上面最后为5,这时--i变为4 第三个i++在4时进行计算,其后变为5 第四个--j,变为6 * * 所以6-4+4-6=0 */ System.out.println(k); ////////////////////////////// int q = 32; int p = 64; int result = --q + p++ - q + p++ + q-- - q; /** * 31+64(65)-31+65(66)+31(30)-30 31+64-31+65+31-30=130 */ System.out.println(result); ////////////////////////////// int x = 6; int y = 9; x = x ^ y; y = x ^ y; x = y ^ x ^ x ^ y; /** * 先取得数值的二进制6[0110], 9[1001] <-简短先用4位 * * x: 0110 1001 = 1111 * * y: 1111 1001 = 0110 * * x: * 0110 * 1111 = 1001 * 1111 ^ 1001 = 0110 ↓ * 0110 ^ 0110 = 0000 * * x:0 */ System.out.println(x); } } 怕自己忘记,贴一下! ~~~~~~~~~~~~~~~ 将编程看作是一门艺术,而不单单是个技术。 敲打的英文字符是我的黑白琴键, 思维图纸画出的是我编写的五线谱。 当美妙的华章响起,现实通往二进制的大门即将被打开。

资源下载

更多资源
Mario

Mario

马里奥是站在游戏界顶峰的超人气多面角色。马里奥靠吃蘑菇成长,特征是大鼻子、头戴帽子、身穿背带裤,还留着胡子。与他的双胞胎兄弟路易基一起,长年担任任天堂的招牌角色。

Spring

Spring

Spring框架(Spring Framework)是由Rod Johnson于2002年提出的开源Java企业级应用框架,旨在通过使用JavaBean替代传统EJB实现方式降低企业级编程开发的复杂性。该框架基于简单性、可测试性和松耦合性设计理念,提供核心容器、应用上下文、数据访问集成等模块,支持整合Hibernate、Struts等第三方框架,其适用范围不仅限于服务器端开发,绝大多数Java应用均可从中受益。

优质分享App

优质分享App

近一个月的开发和优化,本站点的第一个app全新上线。该app采用极致压缩,本体才4.36MB。系统里面做了大量数据访问、缓存优化。方便用户在手机上查看文章。后续会推出HarmonyOS的适配版本。

Sublime Text

Sublime Text

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。

用户登录
用户注册