技术标签: Java
低级别转为高级别,可以实现自动装和拆箱,反之是不行的
public class Test{
public static void main(){
int a=10;
Integer test1 = new Integer(i); // 基本类型 ——→ 封装类
int a = test1.IntValue(); // 封装类 ——→ 基本类型
Integer test2 = a; //自动装箱
a = test2 // 自动拆箱
//int的最大值
System.out.println(Integer.MAX_VALUE);
//int的最小值
System.out.println(Integer.MIN_VALUE);
}
}
public class TestNumber {
public static void main(String[] args) {
int i = 5;
//方法1
String str = String.valueOf(i);
//方法2
Integer it = i;
String str2 = it.toString();
}
}
调用类型封装类的静态静态方法 parseInt(String)
public class TestNumber {
public static void main(String[] args) {
String str = "999";
int i= Integer.parseInt(str);
System.out.println(i);
}
}
public class TestNumber {
public static void main(String[] args) {
float f1 = 5.4f;
float f2 = 5.5f;
四舍五入
System.out.println(Math.round(f1));
System.out.println(Math.round(f2));
得到一个0-1之间的随机浮点数(取不到1)
System.out.println(Math.random());
得到一个0-10之间的随机整数 (取不到10)
System.out.println((int)( Math.random()*10));
开方
System.out.println(Math.sqrt(9));
次方(2的4次方)
System.out.println(Math.pow(2,4));
π
System.out.println(Math.PI);
自然常数
System.out.println(Math.E);
}
}
为了使得同一个java程序的换行符在所有的操作系统中都有一样的表现,使用%n,就可以做到平台无关的换行
package digit;
public class TestNumber {
public static void main(String[] args) {
String name ="盖伦";
int kill = 8;
String title="超神";
//直接使用+进行字符串连接,编码感觉会比较繁琐,并且维护性差,易读性差
String sentence = name+ " 在进行了连续 " + kill + " 次击杀后,获得了 " + title +" 的称号";
System.out.println(sentence);
//使用格式化输出
//%s表示字符串,%d表示数字,%n表示换行
String sentenceFormat ="%s 在进行了连续 %d 次击杀后,获得了 %s 的称号%n";
System.out.printf(sentenceFormat,name,kill,title);
}
}
package character;
public class TestChar {
public static void main(String[] args) {
System.out.println(Character.isLetter('a'));//判断是否为字母
System.out.println(Character.isDigit('a')); //判断是否为数字
System.out.println(Character.isWhitespace(' ')); //是否是空白
System.out.println(Character.isUpperCase('a')); //是否是大写
System.out.println(Character.isLowerCase('a')); //是否是小写
System.out.println(Character.toUpperCase('a')); //转换为大写
System.out.println(Character.toLowerCase('A')); //转换为小写
//String a = 'a'; 不能够直接把一个字符转换成字符串
String a2 = Character.toString('a'); //转换为字符串
}
}
package digit;
import java.util.Scanner;
/**
* 通过Scanner从控制台读取字符串,然后把字符串转换为字符数组
参考的转换方式:
String str = "abc123";
char[] cs = str.toCharArray();
转换为字符数组后,筛选出控制台读取到的字符串中的大写字母和数字,并打印出来
*
*/
public class TestCharacter {
public static void main(String[] args) {
Scanner s = new Scanner(System.in); //调用控制台的输入流
System.out.println("请输入字符串:");
String str = s.nextLine(); //输入数据为字符串
System.out.println("其中大写字母和数字有:");
char[] cs = str.toCharArray(); //字符串转字符数组
for (int i = 0; i < cs.length; i++) {
//遍历数组长度
char c = cs[i]; //声明字符变量接收每一个数组
if (Character.isUpperCase(c) || Character.isDigit(c)) {
System.out.print(c+" "); //输出大写字母和数字
}
}
}
}
字符串即字符的组合,在Java中,字符串是一个类,所以我们见到的字符串都是对象
常见创建字符串手段:
每当有一个字面值出现的时候,虚拟机就会创建一个字符串
调用String的构造方法创建一个字符串对象:String s1 = new String(s2)
通过+加号进行字符串拼接也会创建新的字符串对象:String s3 = s1 + s2 ;
通过字符数组创建字符串
封装类.toString()可以转字符串
String teemo = new String(“提莫”); //创建了两个字符串对象,即teemo 和 “提莫” ,两个字符串对象
package character;
public class TestString {
public static void main(String[] args) {
String garen ="盖伦"; //字面值,虚拟机碰到字面值就会创建一个字符串对象
String teemo = new String("提莫"); //创建了两个字符串对象
char[] cs = new char[]{
'崔','斯','特'};
String hero = new String(cs);// 通过字符数组创建一个字符串对象
String hero3 = garen + teemo;// 通过+加号进行字符串拼接
}
}
String 被修饰为final,所以是不能被继承的
immutable 是指不可改变的,相当于字符串常量,直接字面值创建的字符串对象就是;
string s.length方法返回当前字符串的长度
可以有长度为0的字符串,即空字符串
String sentence = "你好啊啊啊啊啊";
char c = sentence.charAt(0);
String str = "字符串转数组";
char[] cs = str.toCharArray();
package character;
public class TestString {
public static void main(String[] args) {
String sentence = "盖伦,在进行了连续8次击杀后,获得了 超神 的称号";
//截取从第3+1个开始的字符串 (基0)
String subString1 = sentence.substring(3);
System.out.println(subString1);
//截取从第3个开始的字符串 (基0)
//到5-1的位置的字符串
//左闭右开:3+1 到 5(也就是 4 到 5 的字符)
String subString2 = sentence.substring(3,5);
System.out.println(subString2);
}
}
package character;
public class TestString {
public static void main(String[] args) {
String sentence = "盖伦,在进行了连续8次击杀后,获得了 超神 的称号";
//根据,进行分割,得到3个子字符串
String subSentences[] = sentence.split(",");
for (String sub : subSentences) {
System.out.println(sub);
}
trim 去掉首尾空格
package character;
public class TestString {
public static void main(String[] args) {
String sentence = " 盖伦,在进行了连续8次击杀后,获得了 超神 的称号 ";
//去掉首尾空格
System.out.println(sentence.trim());
}
}
//全部变成小写
System.out.println(sentence.toLowerCase());
//全部变成大写
System.out.println(sentence.toUpperCase());
indexOf 返回字符或者子字符串出现的位置
lastIndexOf 最后一次出现的位置
contains 是否包含子字符串
package character;
public class TestString {
public static void main(String[] args) {
String sentence = "盖伦,在进行了连续8次击杀后,获得了超神 的称号";
System.out.println(sentence.indexOf('8')); //字符第一次出现的位置
System.out.println(sentence.indexOf("超神")); //字符串第一次出现的位置
System.out.println(sentence.lastIndexOf("了")); //字符串最后出现的位置
System.out.println(sentence.indexOf(',',5)); //从位置5开始,出现的第一次,的位置
System.out.println(sentence.contains("击杀")); //是否包含字符串"击杀"
}
}
replaceAll 替换所有的
replaceFirst 只替换第一个
package character;
public class TestString {
public static void main(String[] args) {
String sentence = "盖伦,在进行了连续8次击杀后,获得了超神 的称号";
String temp = sentence.replaceAll("击杀", "被击杀"); //替换所有的
temp = temp.replaceAll("超神", "超鬼");
System.out.println(temp);
temp = sentence.replaceFirst(",","");//只替换第一个
System.out.println(temp);
}
}
public static void main(String[] args) {
//把每个单词的首字母变成大写
String sentence="let there be light";
char c ;
String []sen=sentence.split(" ");//分隔成字符串数组
for(int i=0;i<sen.length;i++) {
c=sen[i].charAt(0);//获取首字母
c=Character.toUpperCase(c);//转化为大写
sen[i]=sen[i].replace(sen[i].toCharArray()[0], c);//替换第一个字母
}
System.out.println(sentence);
for(String s:sen) {
System.out.print(s+" ");
}
System.out.println("\n----------------------");
//统计多少个p的字母
String sentence1="peter piper picked a peck of pickled peppers";
sentence1=sentence1.replaceAll(" ", "");
int count=0;
for(int i=0;i<sentence1.length();i++) {
char s=sentence1.charAt(i);//获取每个字母
String str=s+"";
System.out.print(s);
if("p".equals(str)) {
count++;
}
}
System.out.printf("\n一共有%d个p字母",count);
System.out.println("\n--------------------");
//间隔大小写
String str1="lengendary";
String str2="";
char []st=str1.toCharArray();
System.out.println(str1);
for(int i=0;i<st.length;i++) {
if(i%2==0) {
//偶数就转换为大写
st[i]=Character.toUpperCase(st[i]);
}
str2+=Character.toString(st[i]);
}
System.out.println(str2);
System.out.println("\n--------------------");
//把 lengendary 最后一个字母变大写
String str3="lengendary";
String str4="";
char [] st1=str3.toCharArray();
System.out.println(str3);
for(int i=0;i<st1.length;i++) {
if(i==st1.length-1) {
st1[i]=Character.toUpperCase(st1[i]);
}
str4+=Character.toString(st1[i]);
}
System.out.println(str4);
System.out.println("\n--------------------");
//Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak
//把最后一个two单词首字母大写
String sentence3="Nature has given us that two ears, two eyes, and but one tongue, to the end that we should hear and see more than we speak";
// System.out.println(sentence3.lastIndexOf("two")); //获取最后一个two的位置
System.out.println(sentence3);
String sentence4="";
char c1;
for(int i=0;i<sentence3.length();i++) {
c1=sentence3.charAt(sentence3.lastIndexOf("two"));
if(i==sentence3.lastIndexOf("two")) {
c1=Character.toUpperCase(c1);
sentence4+=Character.toString(c1);
}else {
c1=sentence3.charAt(i);
sentence4+=Character.toString(c1);
}
}
System.out.println(sentence4);
}
(1)此时str1 和 str 2 是不一样的
String str1 = "the light";
String str2 = new String(str1);
//==用于判断是否是同一个字符串对象
System.out.println( str1 == str2)//false
(2)一般说来,编译器每碰到一个字符串的字面值,就会创建一个新的对象
所以先创建了一个新的字符串"the light"
但接下一行,编译器发现已经存在现成的"the light",那么就直接拿来使用,而没有进行重复创建
String str1 = "the light";
String str3 = "the light";
System.out.println( str1 == str3);
(1)使用equals进行字符串内容的比较,必须大小写一致
(2)equalsIgnoreCase,忽略大小写判断内容是否一致
package character;
public class TestString {
public static void main(String[] args) {
String str1 = "the light";
String str2 = new String(str1);
String str3 = str1.toUpperCase();
//==用于判断是否是同一个字符串对象
System.out.println( str1 == str2);//false
System.out.println(str1.equals(str2));//完全一样返回true
System.out.println(str1.equals(str3));//大小写不一样,返回false
System.out.println(str1.equalsIgnoreCase(str3));//忽略大小写的比较,返回true
}
}
(3)startsWith //以…开始、endsWith //以…结束
package character;
public class TestString {
public static void main(String[] args) {
String str1 = "the light";
String start = "the";
String end = "Ight";
System.out.println(str1.startsWith(start));//以...开始
System.out.println(str1.endsWith(end));//以...结束
}
创建一个长度是100的字符串数组
使用长度是2的随机字符填充该字符串数组
统计这个字符串数组里重复的字符串有多少种
public class TestString {
//产生随机字符串方法
public static String randomString(int length) {
String result = "";
for (int j = 0; j < length; j++) {
while (true) {
char c = (char) (Math.random() * ('z' - '0' + 1) + '0');
if (Character.isLetterOrDigit(c)) {
result += c;
break;
}
}
}
return result;
}
public static void main(String[] args) {
String[] s = new String[100];
//生成随机字符串
for (int i = 0; i < s.length; i++) {
s[i] = randomString(2);
}
for (int i =0; i<s.length; i++) {
System.out.print(s[i] + " ");
if(i%20 == 19)
System.out.println("");
}
//统计重复字符串
int count = 0;
boolean flag = false;
String result = "";
String[] ss = s; //为了保留原字符串数组s,所以新创建了ss
for (int i = 0; i < ss.length - 1; i++)
for (int j = i + 1; j < ss.length; j++) {
if (ss[i] == " ")
break;
if (ss[i].equalsIgnoreCase(ss[j])) {
ss[j] = " ";
flag = true;
}
if (flag) {
result += ss[i] + " ";
count++;
flag = false;
}
}
System.out.println("\n重复的字符串共有" + count +"种,分别是:\n"+result);
}
}
和String内部是一个字符数组一样,StringBuffer也维护了一个字符数组。 但是,这个字符数组,留有冗余长度
比如说new StringBuffer(“the”),其内部的字符数组的长度,是19,而不是3,这样调用插入和追加,在现成的数组的基础上就可以完成了。
如果追加的长度超过了19,就会分配一个新的数组,长度比原来多一些,把原来的数据复制到新的数组中,看上去 数组长度就变长了 参考MyStringBuffer
length: “the”的长度 3
capacity: 分配的总空间 19
package character;
public class TestString {
public static void main(String[] args) {
String str1 = "let there ";
StringBuffer sb = new StringBuffer(str1); //根据str1创建一个StringBuffer对象
sb.append("be light"); //在最后追加
System.out.println(sb);
sb.delete(4, 10);//删除4-10之间的字符
System.out.println(sb);
sb.insert(4, "there ");//在4这个位置插入 there
System.out.println(sb);
sb.reverse(); //反转
System.out.println(sb);
}
}
package character;
public class TestString {
public static void main(String[] args) {
String str1 = "the";
StringBuffer sb = new StringBuffer(str1);
System.out.println(sb.length()); //内容长度
System.out.println(sb.capacity());//总空间
}
}
生成10位长度的随机字符串
然后,先使用String的+,连接10000个随机字符串,计算消耗的时间
然后,再使用StringBuffer连接10000个随机字符串,计算消耗的时间
有个视屏待看
iOS上面的计步应用都是访问“健康”内的数据,所以只要修改“健康”的数据就可以达到修改QQ或者微信步数的需求,装X神技。 首先打开HealthKit:TARGETS–Capabilities–HealthKit 然后倒入导入HealthKit.framework:TARGETS–Build Phases–Link Binary With Libraries 导入头文件:#import
查看本机maven的版本或是否安装成功,报错,没有权限访问解决办法:chmod a+x /usr/local/apache-maven-3.5.2/bin/mvn (a:所有用户 +:增加权限 x:执行权限)
数学建模集训知识大纲文章目录数学建模集训知识大纲评价算法简单加权法逼近于理想解的排序法(TOPSIS算法)层次分析法主成分分析法模糊综合评价法聚类分析法秩和比法人工神经网络熵权法灰色关联度分析预测算法插值拟合回归模型预测灰色预测GM时间序列神经网络统计分析方差分析回归分析多元统计分析分类问题假设检验(非参数)聚类最优化方法常用算法无约束优化有约束优化二次规划一般有约束非线性规划计算机算法动态规划...
1、x264的码率控制原理与对应源码解析基本概念对于此前了解过H.264的码率控制原理的读者(如果不了解,也可以去看我的文章H.264码率控制算法研究及JM相应代码分析),首先需要说明的是x264采用的码率控制算法并没有采用拉格朗日代价函数来控制编码,而是使用一种更简单的方法,即利用半精度帧的SATD作为量化等级参数选择的依据。SATD即将残差经哈德曼变换的4×4块的预测残差绝对值总和,可以将其看...
1.插件介绍 CSDN的idea插件CSDN tools(以下简称tools),tools整合了日常开发中常用的工具,提高开发效率。 顺便安利下我司的Chrome插件,不要太好使,根本停不下来.jpg 插件主要功能:(小伙伴们有啥想要的功能,尽管砸过来,我们在群里等着你,群地址在文末) json格式化时间格式化ip查询计算器CSDN平台搜索github仓库搜索m...
Face SubstitutionSource Code:https://github.com/arturoc/FaceSubstitutionhttps://vimeo.com/29279198https://github.com/genekogan/ofxLearn高级封装Dlib机器学习https://vimeo.com/29348533https://vimeo.com...
导出到Excel/Csc文件并不难,所以就有好多方法:控件直接Render、把DataSet输出成String再Write出来等,(当然如果调用Excel程序的库文件的话还可以使用更强的直接操作Excel的方法,但这种方法用于Web服务显得有点要求太高:必须让Web服务器安装指定
Logstash 是数据管道,可帮助我们处理来自各种来源的日志和其他事件数据。 Logstash 拥有 200 多个插件,可以连接到各种源并将数据流式传输到中央分析系统。 Elastic Stack(Elasticsearch,Logstash和Kibana)是管理和分析日志和事件的最佳解决方案之一。有效分析和查询发送到 Elastic Stack 的数据的能力取决于数据的可读性和质量。 这意味着,如果将非结构化数据(例如纯文本日志)提取到系统中,则必须将其转换为富含有价值字段的结构化形式。 无论数
我使用的是ReentrantLock锁,所以会有一个Condition对象,当我使用Condition对象唤醒await()的线程时,报错:java.lang.IllegalMonitorStateException这个线程里唤醒代码是这么写的:public void wakeUp(){ IS_FIGHT.set(false); cond.signalAll(); }运行到...
今天使用到了GPU来加速计算,那种感觉简直就是一飞冲天了,临近毕业季,大家都在做实验,服务器早已是不堪重负了,我们屋的服务器一堆人再用,卡到爆炸,训练一个模型粗略计算一下迭代100次就需要3、4天的时间,得不偿失,正好隔壁有一台闲置的GPU深度学习服务器,决定上手搞一搞。 深度学习我也是初步接触,果断选择最简单的keras来入门,网上关于Tensorflow和Theano的GPU加速...
这是我在使用Mxnet过程遇到的一些问题,有的是我自己解决,有的是参考他人的解决方法。在这里做个总结,帮助他人少走些弯路。1、im2rec.py中参数--exts的问题我是使用过程中发现,如果使用默认值输出结果是正确的,如果我们在命令行中自己输入参数就无法输出结果。经过研究发现是im2rec.py源码有问题,作者在使用argparse模块是忽视了一个参数:原始代码如下:cgroup
#import #import @interface ViewController (){ MKMapView *_mapview; CLLocationManager *_manager;}@[email protected] ViewController- (void)viewDidLoad { [super viewDidLoad];