java汉字转化accic_Java自主学习贴
該樓層疑似違規(guī)已被系統(tǒng)折疊?隱藏此樓查看此樓
2019-08-25
鏈表學習續(xù)
實現(xiàn)數(shù)據(jù)內(nèi)容查詢功能
interface ILink{//創(chuàng)建一個接口用于定義方法標準
//定義增加方法
public void add(E e) ;
//定義獲取元素個數(shù)方法
public int getLength();
//判斷是否為空集合
public boolean isEmpty();
//定義返回鏈表數(shù)據(jù)方法(返回數(shù)據(jù)為數(shù)組形式,為了通用性類型設(shè)置為Object)
public Object [] toArray() ;
//定義根據(jù)索引索取數(shù)據(jù)
public E get(int index) ;
//定義修改數(shù)據(jù)方法
public void set(int index, E data) ;
//定義數(shù)據(jù)內(nèi)容查詢功能
public boolean contains(E data) ;
}
class LinkImpl implements ILink{//創(chuàng)建一個子類繼承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("指定索引不在范圍之內(nèi)");
}else{
this.foot = 0 ;
return this.root.getNode(index) ;
}
}
@Override
public void set(int index, E data){
if(index >= this.count){
throw new ArrayIndexOutOfBoundsException("指定索引不在范圍之內(nèi)");
}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) ;
}
}
//-------------------以上為接口子類,以下為內(nèi)部類---------------------------
private class Node{//創(chuàng)建內(nèi)部類用于實現(xiàn)引用關(guān)系的處理
private E data ;//用于節(jié)點保存數(shù)據(jù)
private Node next ;//用于節(jié)點的引用關(guān)系
public Node(E data){//創(chuàng)建節(jié)點是保存數(shù)據(jù)
this.data = data ;
}
//保存新的節(jié)點數(shù)據(jù)
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"));
}
}
實現(xiàn)刪除鏈表數(shù)據(jù)功能
interface ILink{//創(chuàng)建一個接口用于定義方法標準
//定義增加方法
public void add(E e) ;
//定義獲取元素個數(shù)方法
public int getLength();
//判斷是否為空集合
public boolean isEmpty();
//定義返回鏈表數(shù)據(jù)方法(返回數(shù)據(jù)為數(shù)組形式,為了通用性類型設(shè)置為Object)
public Object [] toArray() ;
//定義根據(jù)索引索取數(shù)據(jù)
public E get(int index) ;
//定義修改數(shù)據(jù)方法
public void set(int index, E data) ;
//定義數(shù)據(jù)內(nèi)容查詢功能
public boolean contains(E data) ;
//定義刪除數(shù)據(jù)功能
public void remove(E e) ;
}
class LinkImpl implements ILink{//創(chuàng)建一個子類繼承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("指定索引不在范圍之內(nèi)");
}else{
this.foot = 0 ;
return this.root.getNode(index) ;
}
}
@Override
public void set(int index, E data){
if(index >= this.count){
throw new ArrayIndexOutOfBoundsException("指定索引不在范圍之內(nèi)");
}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 -- ;
}
}
//-------------------以上為接口子類,以下為內(nèi)部類---------------------------
private class Node{//創(chuàng)建內(nèi)部類用于實現(xiàn)引用關(guān)系的處理
private E data ;//用于節(jié)點保存數(shù)據(jù)
private Node next ;//用于節(jié)點的引用關(guān)系
public Node(E data){//創(chuàng)建節(jié)點是保存數(shù)據(jù)
this.data = data ;
}
//保存新的節(jié)點數(shù)據(jù)
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"));
}
}
總結(jié)
以上是生活随笔為你收集整理的java汉字转化accic_Java自主学习贴的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 生鲜电商进入2.0时代,美团还有“后招”
- 下一篇: 微信、支付宝迎劲敌?华为拿下支付牌照