1、Java的String类使用

1. String类的比较

(1)使用“==”是用来进行地址比较的。

(2)如果要判断两个字符串是否相等,String提供专门的方法:public boolean equals(String str)

2. 共享设计

在Java中形成一个对象池,在这个对象池中保存多个对象,新实例化的对象如果已经在池中定义,则不必重新定义,而从池中直接取出继续使用。

3. 字符串常用方法

public char charAt(int index)
//返回字符串中第index个字符;
public int length()
//返回字符串的长度;
public int indexOf(String str)
//返回字符串中第一次出现str的位置;
public int indexOf(String str, int fromIndex)
//返回字符串从fromIndex开始第一次出现str的位置;
public boolean equalsIgnoreCase(String another)
//比较字符串与another是否一样(忽略大小写);
public String replace(char oldchar, char newChar)
//在字符串中用newChar字符替换oldChar字符
public boolean startsWith(String prefix)
//判断字符串是否以prefix字符串开头;
public boolean endsWith(String suffix)
//判断一个字符串是否以suffix字符串结尾;
public String toUpperCase()
//返回一个字符串为该字符串的大写形式;
public String toLowerCase()
//返回一个字符串为该字符串的小写形式
public String substring(int beginIndex)
//返回该字符串从beginIndex开始到结尾的子字符串;
public String substring(int beginIndex, int endIndex)
//返回该字符串从beginIndex开始到endsIndex结尾的子字符串
public String trim()
//返回该字符串去掉开头和结尾空格后的字符串
public String[] split(String regex)
//将一个字符串按照指定的分隔符分隔,返回分隔后的字符串数组

4. 字符串的引用传递

字符串的内容一旦声明是不可改变的,改变的只是其内存地址的指向。如果要进行字符串的传递,可以通过类封装来实现。

class StringData
{
    String tmp = "hello";
}

5. Java中常用的内存区域

在Java中主要存在4块内存空间,这些内存空间的名称及其作用下。

  • 栈内存空间:保存所有的对象名称(更准确地说是保存了引用的堆内存空间的地址)。
  • 堆内存空间:保存每个对象的具体属性内容。
  • 全局数据区:保存static类型的属性。
  • 全局代码区:保存所有的方法的定义。
//一个单例模式的实现
class Singleton
{
    private static Singleton instance = new Singleton();
    private Singleton()
    {
    }
    public static Singleton getInstance()
    {
        return instance;
    }
    public void print()
    {
        System.out.println("Hello World");
    }
}

6. Java的链表实现

public class LinkedList<E>
{
    class Node
    {
        private E data;
        private Node next;
        public Node(E data)
        {
            this.data = data;
        }
        public void add(Node newNode)
        {
            if(this.next == null)
            {
                this.next = newNode;
            }
            else
            {
                this.next.add(newNode);
            }
        }
        public void print()
        {
            System.out.println(this.data + "\t");
            if(this.next != null)
                this.next.print();
        }
        public boolean search(E data)
        {
            if(data.equals(this.data))
            {
                return true;
            }
            else
            {
                if(this.next != null)
                {
                    return this.next.search(data);
                }
                else
                {
                    return false;
                }
            }
        }
        public void delete(Node prev, E data)
        {
            if(data.equals(this.data))
            {
                prev.next = this.next;
            }
            else
            {
                if(this.next != null)
                {
                    this.next.delete(this, data);
                }
            }
        }

    };

    private Node root;//表示根节点
    public void addNode(E data)
    {
        //建立一个新节点
        Node newNode = new Node(data);
        //没有根节点
        if(this.root == null)
        {
            //将第一个节点设置成根节点
            this.root = newNode;
        }
        else
        {
            //添加到合适的位置
            this.root.add(newNode);
        }
    }
    public void printNode()
    {
        //递归打印
        if(this.root != null)
        {
            this.root.print();
        }
    }
    public boolean contains(E name)
    {
        return this.root.search(name);
    }
    public void deleteNode(E data)
    {
        if(this.contains(data))
        {
            //如果节点存在则执行删除操作
            if(this.root.data.equals(data))
            {
                this.root = this.root.next;
            }
            else
            {
                this.root.next.delete(root, data);
            }
        }
    }

    public static void main(String args[])
    {
        LinkedList list = new LinkedList();
        list.addNode("A");
        list.addNode("B");
        list.printNode();

        list.deleteNode("A");
        list.printNode();
    }

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值