java汉字转化accic_Java自主学习贴
該樓層疑似違規已被系統折疊?隱藏此樓查看此樓
2019-08-25
鏈表學習續
實現數據內容查詢功能
interface ILink{//創建一個接口用于定義方法標準
//定義增加方法
public void add(E e) ;
//定義獲取元素個數方法
public int getLength();
//判斷是否為空集合
public boolean isEmpty();
//定義返回鏈表數據方法(返回數據為數組形式,為了通用性類型設置為Object)
public Object [] toArray() ;
//定義根據索引索取數據
public E get(int index) ;
//定義修改數據方法
public void set(int index, E data) ;
//定義數據內容查詢功能
public boolean contains(E data) ;
}
class LinkImpl implements ILink{//創建一個子類繼承ILink接口
private Node root ;
@Override
public void add(E e){
if(e == null){
return ;
}
Node newNode = new Node(e);
if(this.root == null){
this.root = newNode ;
}else{
this.root.addNode(newNode) ;
}
this.count ++ ;
}
private int count ;
@Override
public int getLength(){
return this.count ;
}
@Override
public boolean isEmpty(){
if (this.count == 0){
return true ;
}else{
return false ;
}
}
private int foot ;
private Object [] returnData ;
@Override
public Object [] toArray(){
if(this.isEmpty()){
throw new NullPointerException("空集合");
}
this.foot = 0 ;
this.returnData = new Object [this.count] ;
this.root.toArrayNode();
return this.returnData ;
}
@Override
public E get(int index){
if(index >= this.count){
throw new ArrayIndexOutOfBoundsException("指定索引不在范圍之內");
}else{
this.foot = 0 ;
return this.root.getNode(index) ;
}
}
@Override
public void set(int index, E data){
if(index >= this.count){
throw new ArrayIndexOutOfBoundsException("指定索引不在范圍之內");
}else{
this.foot = 0 ;
this.root.setNode(index,data) ;
}
}
@Override
public boolean contains(E data){
if(data == null){
return false ;
}else{
return this.root.containsNode(data) ;
}
}
//-------------------以上為接口子類,以下為內部類---------------------------
private class Node{//創建內部類用于實現引用關系的處理
private E data ;//用于節點保存數據
private Node next ;//用于節點的引用關系
public Node(E data){//創建節點是保存數據
this.data = data ;
}
//保存新的節點數據
public void addNode(Node newNode){
if(this.next == null){
this.next = newNode ;
}else{
this.next.addNode(newNode) ;
}
}
public void toArrayNode(){
LinkImpl.this.returnData[LinkImpl.this.foot ++] = this.data ;
if(this.next != null){
this.next.toArrayNode() ;
}
}
public E getNode(int index){
if(LinkImpl.this.foot ++ == index){
return this.data ;
}else{
return this.next.getNode(index) ;
}
}
public void setNode(int index, E data){
if(LinkImpl.this.foot ++ == index){
this.data = data ;
}else{
this.next.setNode(index,data) ;
}
}
public boolean containsNode(E data){
if(this.data.equals(data)){
return true ;
}else{
if(this.next == null){
return false ;
}else{
return this.next.containsNode(data) ;
}
}
}
}
}
public class LinkDemo{
public static void main(String args[]){
ILink link = new LinkImpl () ;
link.add("Hello");
link.add("World");
link.add("Allan");
link.add("Tom");
System.out.println(link.getLength()) ;
System.out.println(link.isEmpty()) ;
link.set(2,"你好!!!") ;
System.out.println(link.get(2));
System.out.println(link.contains("你好!!!"));
System.out.println(link.contains("2212"));
}
}
實現刪除鏈表數據功能
interface ILink{//創建一個接口用于定義方法標準
//定義增加方法
public void add(E e) ;
//定義獲取元素個數方法
public int getLength();
//判斷是否為空集合
public boolean isEmpty();
//定義返回鏈表數據方法(返回數據為數組形式,為了通用性類型設置為Object)
public Object [] toArray() ;
//定義根據索引索取數據
public E get(int index) ;
//定義修改數據方法
public void set(int index, E data) ;
//定義數據內容查詢功能
public boolean contains(E data) ;
//定義刪除數據功能
public void remove(E e) ;
}
class LinkImpl implements ILink{//創建一個子類繼承ILink接口
private Node root ;
@Override
public void add(E e){
if(e == null){
return ;
}
Node newNode = new Node(e);
if(this.root == null){
this.root = newNode ;
}else{
this.root.addNode(newNode) ;
}
this.count ++ ;
}
private int count ;
@Override
public int getLength(){
return this.count ;
}
@Override
public boolean isEmpty(){
if (this.count == 0){
return true ;
}else{
return false ;
}
}
private int foot ;
private Object [] returnData ;
@Override
public Object [] toArray(){
if(this.isEmpty()){
throw new NullPointerException("空集合");
}
this.foot = 0 ;
this.returnData = new Object [this.count] ;
this.root.toArrayNode();
return this.returnData ;
}
@Override
public E get(int index){
if(index >= this.count){
throw new ArrayIndexOutOfBoundsException("指定索引不在范圍之內");
}else{
this.foot = 0 ;
return this.root.getNode(index) ;
}
}
@Override
public void set(int index, E data){
if(index >= this.count){
throw new ArrayIndexOutOfBoundsException("指定索引不在范圍之內");
}else{
this.foot = 0 ;
this.root.setNode(index,data) ;
}
}
@Override
public boolean contains(E data){
if(data == null){
return false ;
}else{
return this.root.containsNode(data) ;
}
}
@Override
public void remove(E data){
if(this.contains(data)){
if(this.root.data.equals(data)){
this.root = this.root.next ;
}else{
this.root.next.removeNode(this.root, data) ;
}
this.count -- ;
}
}
//-------------------以上為接口子類,以下為內部類---------------------------
private class Node{//創建內部類用于實現引用關系的處理
private E data ;//用于節點保存數據
private Node next ;//用于節點的引用關系
public Node(E data){//創建節點是保存數據
this.data = data ;
}
//保存新的節點數據
public void addNode(Node newNode){
if(this.next == null){
this.next = newNode ;
}else{
this.next.addNode(newNode) ;
}
}
public void toArrayNode(){
LinkImpl.this.returnData[LinkImpl.this.foot ++] = this.data ;
if(this.next != null){
this.next.toArrayNode() ;
}
}
public E getNode(int index){
if(LinkImpl.this.foot ++ == index){
return this.data ;
}else{
return this.next.getNode(index) ;
}
}
public void setNode(int index, E data){
if(LinkImpl.this.foot ++ == index){
this.data = data ;
}else{
this.next.setNode(index,data) ;
}
}
public boolean containsNode(E data){
if(this.data.equals(data)){
return true ;
}else{
if(this.next == null){
return false ;
}else{
return this.next.containsNode(data) ;
}
}
}
public void removeNode(Node previous, E data){
if(this.data.equals(data)){
previous.next = this.next ;
}else{
if(this.next != null){
this.next.removeNode(this, data) ;
}
}
}
}
}
public class LinkDemo{
public static void main(String args[]){
ILink link = new LinkImpl () ;
link.add("Hello");
link.add("World");
link.add("Allan");
link.add("Tom");
System.out.println(link.getLength()) ;
System.out.println(link.isEmpty()) ;
link.remove("Tom") ;
Object [] results = link.toArray() ;
for(Object obj : results){
System.out.println(obj) ;
}
link.set(2,"你好!!!") ;
System.out.println(link.get(2));
System.out.println(link.contains("你好!!!"));
System.out.println(link.contains("2212"));
}
}
總結
以上是生活随笔為你收集整理的java汉字转化accic_Java自主学习贴的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 生鲜电商进入2.0时代,美团还有“后招”
- 下一篇: java美元兑换,(Java实现) 美元