1.将字符串变成整数,一般在算法中涉及到大数计算时需要用到
class test { public static void main(String[] args) { String s="233123"; int i=Integer.parseInt(s); //当s里面有非数字的时候会出错 System.out.println(i); } }
2.对于负数的处理,先判断是否为负,然后去掉负号进行计算,在计算完之后加上
class test { public static void main(String[] args) { String s="-233123"; int i=Integer.parseInt(s);//float sourceF = s.parseFloat(sourceStr);变为浮点数 //System.out.println("Hello World!"+i); char frist = s.charAt(0); //获取字符串第一个字符 // if(frist=='-'){System.out.println(frist);}判断第一个字符是否是- //System.out.println(frist); String leave = s.substring(1,s.length()); //截取除第一个字符串之外的所有字符 //System.out.println(leave); String newS = String.valueOf(i); //将整数变为字符串 System.out.println(newS); String add = "-"+s; //拼接字符串 // System.out.println(add); } }
3.java对链表的处理,注意重点是static的相关知识
/** * @author luochengcheng * 定义一个单链表 */ class Node { //变量 private int record; //指向下一个对象 private Node nextNode; public Node(int record) { //初始化 //super(); this.record = record; } public int getRecord() { return record; } public void setRecord(int record) { this.record = record; } public Node getNextNode() { return nextNode; //;得倒下一个节点 } public void setNextNode(Node nextNode) { this.nextNode = nextNode; //指向下一个节点 } } /** * @author luochengcheng * 两种方式实现单链表的反转(递归、普通) * 新手强烈建议旁边拿着纸和笔跟着代码画图(便于理解) */ public class lianbiao { /** * 递归,在反转当前节点之前先反转后续节点 */ public static Node reverse(Node head) { // 比如常见的main方法都是静态的,必须由static修饰,因此在main方法里调用类的其他非静态方法,都是需要先申明对象,才能用。否则就会出现引用非静态方法的错误。 if (null == head || null == head.getNextNode()) { //本节点和下一个节点是否为空 return head; } Node reversedHead = reverse(head.getNextNode()); head.getNextNode().setNextNode(head); //设置翻转链表的倒着连起来 head.setNextNode(null); return reversedHead; } /** * 遍历,将当前节点的下一个节点缓存后更改当前节点指针 * */ public static Node reverse2(Node head) { if (null == head) { return head; } Node pre = head; //标记前一个节点 Node cur = head.getNextNode(); //指针指向后一个节点 Node next; while (null != cur) { next = cur.getNextNode(); cur.setNextNode(pre); //后一个节点设置为前一个节点,链接变了,然后交换完毕之后,将原头节点的下一个节点设为空,为节点为head pre = cur; cur = next; } //将原链表的头节点的下一个节点置为null,再将反转后的头节点赋给head head.setNextNode(null); head = pre; return head; } public static void main(String[] args) { Node head = new Node(0); Node tmp = null; //临时变量 Node cur = null; //指针 // 构造一个长度为10的链表,保存头节点对象head for (int i = 1; i < 10; i++) { tmp = new Node(i-1); if (1 == i) { head.setNextNode(tmp); } else { cur.setNextNode(tmp); } cur = tmp; } //打印反转前的链表 Node h = head; while (null != h) { System.out.print(h.getRecord() + " "); h = h.getNextNode(); } //调用反转方法 head = reverse(head); System.out.println(" **************************"); //打印反转后的结果 while (null != head) { System.out.print(head.getRecord() + " "); head = head.getNextNode(); } } }
4.Java的正则表达式的运用
注意3点
1.import java.util.regex.Matcher; import java.util.regex.Pattern;这两个正则表达式的包
2.import java.util.ArrayList;arraylist的包的引入
3.arraylist size()判断数组的长度,add是添加元素,get(),是获得第几个元素
import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.ArrayList; public class zhence { public static void main(String[] args) { ArrayList<String> list = new ArrayList<String>(); //声明一个字符串数组,记得应用了ArrayList包 Pattern sp = Pattern.compile("\d+"); //compile 规则 String s = "123aah34345bb-234cc-00"; //匹配字符串 Matcher n = sp.matcher(s); //开始匹配 // p(n.groupCount());//2组 while(n.find()){ list.add(n.group()); //放到数组里面去 // p(n.group());//数字字母都有 // p(n.group(1));//只有数字模式1 // p(n.group(2));//只有字母模式2 } if(0==list.size()){ System.out.println("数组不存在"); }else{ System.out.println(list.get(0)); //获得字符串中的第一个数字 } } // public static void p(Object o){ // System.out.println(o); // } }
5.解决键盘输入问题
import java.io.*; class shuru { public static void main(String [] args) throws IOException{ BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = null; System.out.println("Enter your value:"); str = br.readLine(); System.out.println("your value is :"+str); } }