首页 文章 精选 留言 我的

精选列表

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

Java 面向对象 之 对象数组

http://www.verejava.com/?id=16992784958543 /** 知识点: 对象数组 1. 对象数组的使用 2. 对象数组的foreach 增强for循环 3. 可变参数 题目:乘客一次只能带2个箱子免费上飞机 思路: 1. 抽象出类 乘客(Customer) , 箱子(Box) 2. 乘客和箱子的关系 1对2的关系 Box->Customer */ public class ObjectArray { public static void main(String[] args) { //实例化一个乘客 Customer c = new Customer("黎明"); //实例化两个箱子一个装衣服, 一个装书 Box clothBox = new Box("装衣服"); clothBox.setId(1); clothBox.setWeight(20); Box bookBox = new Box("装书"); bookBox.setId(2); bookBox.setWeight(30); //将箱子添加到乘客 c.addBox(clothBox); c.addBox(bookBox); //打印该乘客的信息 System.out.println("乘客姓名: " + c.getName()); System.out.println("箱子编号, 箱子重量, 箱子描述"); Box[] boxes = c.getBoxes(); for (Box box : boxes) { System.out.println(box.getId() + "," + box.getWeight() + "," + box.getDescription()); } //测试可变参数 Box[] boxes2 = { clothBox, bookBox }; c.setBoxes();//不传参数 c.setBoxes(clothBox);//传一个参数 c.setBoxes(boxes2);//传一个数组 boxes = c.getBoxes(); for (Box box : boxes) { System.out.println(box.getId() + "," + box.getWeight() + "," + box.getDescription()); } } } class Customer { private String name;//乘客名字 private Box[] boxes = new Box[2];//箱子属于乘客, 添加箱子引用 public Customer(String name) { this.name = name; } public String getName() { return this.name; } public void setName(String name) { this.name = name; } public Box[] getBoxes() { return this.boxes; } //乘客携带添加的箱子 //返回值: 如果添加成功返回true 否则false public boolean addBox(Box box) { for (int i = 0; i < boxes.length; i++) { if (boxes[i] == null) { boxes[i] = box; return true; } } return false; } public void setBoxes(Box... boxes) { this.boxes = boxes; } } class Box { private float weight;//箱子的重量 private int id;//箱子的编号 private String description;//描述 public Box(String description) { this.description = description; } public String getDescription() { return this.description; } public void setDescription(String description) { this.description = description; } public int getId() { return this.id; } public void setId(int id) { this.id = id; } public float getWeight() { return this.weight; } public void setWeight(float weight) { this.weight = weight; } } http://www.verejava.com/?id=16992784958543

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

Java 面向对象 之 代码块

http://www.verejava.com/?id=16992781354342 /** 知识点: 代码块 1. 普通代码块 2. 构造代码块 3. 静态代码块 */ public class CodeBlock { //写在构造方法里的叫 构造代码块 public CodeBlock() { System.out.println("CodeBlock 初始化"); } //写在 static 块里的叫 静态代码块 static { System.out.println("static 执行"); } public static void main(String[] args) { //写在方法里的叫 普通代码块 System.out.println("hello"); new CodeBlock(); } } /** 注意: 静态代码块是在程序编译期运行 所以静态代码块会在构造方法前调用 */ http://www.verejava.com/?id=16992781354342

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

Java 面向对象 之 引用传递

http://www.verejava.com/?id=16992763271038 /** 知识点内容: 引用传递 1. 方法参数引用传递 2. 对象与对象之间的引用传递 3. this指针 本对象引用传递 */ public class Refenrence { public static void main(String[] args) { //实例化 工具类 Utils Utils utils = new Utils(); //定义一个计数器个变量 int count = 1; System.out.println(count); utils.increment(count); System.out.println(count); //定义一个计数器数组变量 int[] counts = { 1 }; System.out.println(counts[0]); utils.increment(counts); System.out.println(counts[0]); //定义一个计数器类变量 Count value = new Count(); value.setValue(1); System.out.println(value.getValue()); utils.increment(value); System.out.println(value.getValue()); } } //建一个工具类 class Utils { public void increment(int count) { count++; } public void increment(int[] counts) { counts[0]++; } public void increment(Count count) { count.setValue(count.getValue() + 1); } } class Count { private int value; public int getValue() { return this.value; } public void setValue(int value) { this.value = value; } } http://www.verejava.com/?id=16992763271038

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

Java 面向对象 之 封装方法

http://www.verejava.com/?id=16992728331734 /** 测试类的封装特性 类的封装特性就是, 将类的属性私有化private 然后提供公有的方法去访问 */ public class Encapsulation { public static void main(String[] args) { //实例化Person 类 //1. 引用对象不能访问类的私有属性 //2. 如果要访问私有属性, 一般类中要另外提供访问私有属性的公有方法 Person p = new Person(); //p.name="李明"; //p.age=22; p.setName("李明"); p.setAge(22); p.say(); System.out.println(p); System.out.println(p.getName() + " " + p.getAge()); } } class Person { //实现类属性的封装 前面加 private 关键字 private String name; private int age; //定义访问私有属性的公有方法 //括号中 String name 是 方法setName 的传入参数 public void setName(String name) { //this 代表当前对象的引用, 哪个对象引用该类,就是指的哪个引用 System.out.println(this); this.name = name; } //String 是方法getName 的返回值的类型 输出返回值 public String getName() { return this.name; } public void setAge(int age) { this.age = age; } public int getAge() { return this.age; } public void say() { System.out.println("我的名字叫:" + name + ",今年 " + age + " 岁"); } } /* 回顾总结 1. 在类当中定义的变量叫成员变量, 属性就是成员变量, 可以在整个类当中访问 2. 在方法中定义的变量叫局部变量, 只能在定义的方法当中访问 注意: 当局部变量和成员变量同名的时候, 局部变量回覆盖掉成员变量,所以当两个变量同名时 要区分成员变量必须加上 this */ http://www.verejava.com/?id=16992728331734

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

Java 目录和文件的复制

1.复制一个目录及其子目录、文件到另外一个目录 //复制一个目录及其子目录、文件到另外一个目录 public void copyFolder(File src, File dest) throws IOException { if (src.isDirectory()) { if (!dest.exists()) { dest.mkdir(); } String files[] = src.list(); for (String file : files) { File srcFile = new File(src, file); File destFile = new File(dest, file); // 递归复制 copyFolder(srcFile, destFile); } } else { InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dest); byte[] buffer = new byte[1024]; int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } in.close(); out.close(); } } private void copyFolder(File src, File dest) throws IOException { if (src.isDirectory()) { if (!dest.exists()) { dest.mkdir(); } String files[] = src.list(); for (String file : files) { File srcFile = new File(src, file); File destFile = new File(dest, file); // 递归复制 copyFolder(srcFile, destFile); } } else { InputStream in = new FileInputStream(src); OutputStream out = new FileOutputStream(dest); byte[] buffer = new byte[ 1024]; int length; while ((length = in.read(buffer)) > 0) { out.write(buffer, 0, length); } in.close(); out.close(); } }

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

Java----------LeetCode----------46. 全排列

说起回溯算法,渊源颇深,我上次做过一道,本应该这次能拿下但是,又没拿下,不过这次有点理解它的思想了, 每次的回溯就是一个二叉树,简单的说,就是你执行的操作需要回溯时它会返回调用它的方法哪里的状态,进行下一步的执行。 public List<List<Integer>> permute(int[] nums) { List<List<Integer>> temp = new ArrayList<>(20); zuhe(temp,0,nums); return temp; } private void zuhe(List<List<Integer>> temp, int n, int[] nums) { /* * 每次让其加一个就行了,就空了, * */ if (nums.length == n) { List<Integer> integers =new ArrayList<>(20); for (int r:nums){ integers.add(r); } temp.add(integers); } for (int i = n, length = nums.length; i <length ; i++) { change(nums,i,n); zuhe(temp,n+1,nums); change(nums,i,n); } } public void change(int[] nums, int i, int j) { int temp = nums[i]; nums[i] = nums[j]; nums[j] = temp; } 这个需要多练,具体自己体会

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

java之 ------------[LeetCode] House Robber 打家劫舍||

做完打家劫舍后我发现自己动态规划方面处理问题的能力,终于迎来了开篇,虽然打家劫舍是在我看网上的别的人做的,然后自己理解的,但是我知道我再遇到这类题不会再手足无措了,隔了两天再来挑战,我看看自己的动态规划能力是否有那么一点点,于是做了打家劫舍||,虽然我做了将近2个小时,但是庆幸的是自己依靠自己的能力做了出来,很感动,自己花了一晚上的时间做出来了,我都被自己感动的哭了,我算法如此垃圾,竟然能完全依靠自己的能力做出这个算法,真的很让我相信:天才是少数的,大多数人喜欢给自己的懒,找借口。好了不说, 说思路吧: 1.不让它收尾都取,只让它取其一,首端取了,尾端就不取,尾端取了首端就不取,然后在打家劫舍的基础上多声明一个数组,这个数组的取值是从原数组的尾端开始取值的,其实算法和打家劫舍一样,只不过我把打家劫舍的算法用了两遍, 2.上代码吧 public int rob(int[] nums) { if (nums.length == 0) { return 0; } if (nums.length == 1) { return nums[0]; } int max = Math.max(nums[0],nums[1]); if (nums.length == 2) { return max; } int[] f = new int[nums.length]; int[] n = new int[nums.length]; f[0] = nums[0]; f[1] = max; n[nums.length-1] = nums[nums.length-1]; n[nums.length-2] = Math.max(nums[nums.length-1],nums[nums.length-2]); for (int i = 2; i <nums.length-1 ; i++) { f[i] = Math.max(f[i - 2] + nums[i], f[i - 1]); } for (int i = nums.length-3; i > 0; i--) { n[i] = Math.max(n[i+2] + nums[i], n[i+1]); } if (f[nums.length-2] >= n[1]){ return f[nums.length-2]; } return n[1]; }

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

Java 数组 之 一维数组

http://www.verejava.com/?id=16992640551624 /** 数组分类 1. 一维数组 1.1 一维数组的定义和初始化 1.2 对一维数组的操作, 遍历,添加,插入,修改,删除,排序,查找 2. 二维数组 2.1 二维数组的定义和初始化 2.2 二维数组的遍历 3. 多维数组 4. 增强for循环 */ public class Array { public static void main(String[] args) { //一维数组的定义和初始化 //静态定义一维数组 int[] scores = { 90, 70, 50, 80, 60, 85 }; //动态定义一维数组 int[] arr = new int[6]; arr[0] = 90; arr[1] = 70; arr[2] = 50; arr[3] = 80; arr[4] = 60; arr[5] = 85; //一维数组遍历,打印出数组scores的成绩 for (int i = 0; i < scores.length; i++) { System.out.print(scores[i] + ","); } } } //数组为字符串1 public class Test1 { public static void main(String[] args) { // 书架上放了一组图书, 打印出这组图书的书名 //创建一个存放书的书架, 定义一个空的存放书的数组 0,1,2,3 String[] books = new String[4]; // 4是数组的长度 //将书存入数组 books books[0] = "乔布斯传"; books[1] = "从优秀到卓越"; books[2] = "人生不设限"; books[3] = "态度决定一切"; //打印出这组图书的书名 //System.out.println(books[2]); //数组的遍历 System.out.println(books.length); //数组的元素个数 for (int i = 0; i < books.length; i++) { System.out.println(books[i]); } } } //数组为字符串 2 public class Test2 { public static void main(String[] args) { //定义一个初始化的数组 int a = 0; String[] books = { "乔布斯传", "从优秀到卓越", "人生不设限", "态度决定一切" }; for (int i = 0; i < books.length; i++) { System.out.println(books[i]); } //王涛中考 考了 语文 ,数学,英语 分别为 90, 95, 100 int[] scores = { 90, 95, 100 }; for (int i = 0; i < scores.length; i++) { System.out.println(scores[i]); } } } http://www.verejava.com/?id=16992640551624

资源下载

更多资源
Nacos

Nacos

Nacos /nɑ:kəʊs/ 是 Dynamic Naming and Configuration Service 的首字母简称,一个易于构建 AI Agent 应用的动态服务发现、配置管理和AI智能体管理平台。Nacos 致力于帮助您发现、配置和管理微服务及AI智能体应用。Nacos 提供了一组简单易用的特性集,帮助您快速实现动态服务发现、服务配置、服务元数据、流量管理。Nacos 帮助您更敏捷和容易地构建、交付和管理微服务平台。

Rocky Linux

Rocky Linux

Rocky Linux(中文名:洛基)是由Gregory Kurtzer于2020年12月发起的企业级Linux发行版,作为CentOS稳定版停止维护后与RHEL(Red Hat Enterprise Linux)完全兼容的开源替代方案,由社区拥有并管理,支持x86_64、aarch64等架构。其通过重新编译RHEL源代码提供长期稳定性,采用模块化包装和SELinux安全架构,默认包含GNOME桌面环境及XFS文件系统,支持十年生命周期更新。

Sublime Text

Sublime Text

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

WebStorm

WebStorm

WebStorm 是jetbrains公司旗下一款JavaScript 开发工具。目前已经被广大中国JS开发者誉为“Web前端开发神器”、“最强大的HTML5编辑器”、“最智能的JavaScript IDE”等。与IntelliJ IDEA同源,继承了IntelliJ IDEA强大的JS部分的功能。

用户登录
用户注册