java列表新加節(jié)點(diǎn)代碼 java新建一個(gè)列表

java 動(dòng)態(tài)的給樹(shù)添加新節(jié)點(diǎn) 望高手指點(diǎn)啊

//先選中節(jié)點(diǎn)才能增加節(jié)點(diǎn)

讓客戶(hù)滿(mǎn)意是我們工作的目標(biāo),不斷超越客戶(hù)的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶(hù),將通過(guò)不懈努力成為客戶(hù)在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名注冊(cè)、虛擬空間、營(yíng)銷(xiāo)軟件、網(wǎng)站建設(shè)、達(dá)拉特網(wǎng)站維護(hù)、網(wǎng)站推廣。

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.event.*;

import javax.swing.tree.*;

public class TreeTest implements ActionListener,TreeModelListener{

JLabel label=null;

JTree tree=null;

DefaultTreeModel treeModel=null;

String nodeName=null;//原有節(jié)點(diǎn)名稱(chēng)

public TreeTest(){

JFrame f=new JFrame("TreeTest");

Container contentPane=f.getContentPane();

DefaultMutableTreeNode root=new DefaultMutableTreeNode("資源管理器");

tree=new JTree(root);

tree.setEditable(true);

tree.addMouseListener(new MouseHandle());

treeModel=(DefaultTreeModel)tree.getModel();

treeModel.addTreeModelListener(this);

JScrollPane scrollPane=new JScrollPane();

scrollPane.setViewportView(tree);

JPanel panel=new JPanel();

JButton b=new JButton("新增節(jié)點(diǎn)");

b.addActionListener(this);

panel.add(b);

b=new JButton("刪除節(jié)點(diǎn)");

b.addActionListener(this);

panel.add(b);

b=new JButton("清除所有節(jié)點(diǎn)");

b.addActionListener(this);

panel.add(b);

label=new JLabel("Action");

contentPane.add(panel,BorderLayout.NORTH);

contentPane.add(scrollPane,BorderLayout.CENTER);

contentPane.add(label,BorderLayout.SOUTH);

f.pack();

f.setVisible(true);

f.addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent e){

System.exit(0);

}

});

}

//本方法運(yùn)行新增、刪除、清除所有節(jié)點(diǎn)的程序代碼.

public void actionPerformed(ActionEvent ae){

if (ae.getActionCommand().equals("新增節(jié)點(diǎn)")){

DefaultMutableTreeNode parentNode=null;

DefaultMutableTreeNode newNode=new DefaultMutableTreeNode("新節(jié)點(diǎn)");

newNode.setAllowsChildren(true);

TreePath parentPath=tree.getSelectionPath();

//取得新節(jié)點(diǎn)的父節(jié)點(diǎn)

parentNode=(DefaultMutableTreeNode)(parentPath.getLastPathComponent());

//由DefaultTreeModel的insertNodeInto()方法增加新節(jié)點(diǎn)

treeModel.insertNodeInto(newNode,parentNode,parentNode.getChildCount());

//tree的scrollPathToVisible()方法在使Tree會(huì)自動(dòng)展開(kāi)文件夾以便顯示所加入的新節(jié)點(diǎn)。若沒(méi)加這行則加入的新節(jié)點(diǎn)

//會(huì)被 包在文件夾中,你必須自行展開(kāi)文件夾才看得到。

tree.scrollPathToVisible(new TreePath(newNode.getPath()));

label.setText("新增節(jié)點(diǎn)成功");

}

if (ae.getActionCommand().equals("刪除節(jié)點(diǎn)")){

TreePath treepath=tree.getSelectionPath();

if (treepath!=null){

//下面兩行取得選取節(jié)點(diǎn)的父節(jié)點(diǎn).

DefaultMutableTreeNode selectionNode=(DefaultMutableTreeNode)treepath.getLastPathComponent();

TreeNode parent=(TreeNode)selectionNode.getParent();

if (parent!=null) {

//由DefaultTreeModel的removeNodeFromParent()方法刪除節(jié)點(diǎn),包含它的子節(jié)點(diǎn)。

treeModel.removeNodeFromParent(selectionNode);

label.setText("刪除節(jié)點(diǎn)成功");

}

}

}

if (ae.getActionCommand().equals("清除所有節(jié)點(diǎn)")){

//下面一行,由DefaultTreeModel的getRoot()方法取得根節(jié)點(diǎn).

DefaultMutableTreeNode rootNode=(DefaultMutableTreeNode)treeModel.getRoot();

//下面一行刪除所有子節(jié)點(diǎn).

rootNode.removeAllChildren();

//刪除完后務(wù)必運(yùn)行DefaultTreeModel的reload()操作,整個(gè)Tree的節(jié)點(diǎn)才會(huì)真正被刪除.

treeModel.reload();

label.setText("清除所有節(jié)點(diǎn)成功");

}

}

public void treeNodesChanged(TreeModelEvent e){

TreePath treePath=e.getTreePath();

DefaultMutableTreeNode node=(DefaultMutableTreeNode)treePath.getLastPathComponent();

try{

int[] index=e.getChildIndices();

node=(DefaultMutableTreeNode)node.getChildAt(index[0]);

}catch(NullPointerException exc){}

label.setText(nodeName+"更改數(shù)據(jù)為:"+(String)node.getUserObject());

}

public void treeNodesInserted(TreeModelEvent e){

System.out.println("new node insered");

}

public void treeNodesRemoved(TreeModelEvent e){

System.out.println("node deleted");

}

public void treeStructureChanged(TreeModelEvent e){

System.out.println("Structrue changed");

}

public static void main(String[] args){

new TreeTest();

}

class MouseHandle extends MouseAdapter{

public void mousePressed(MouseEvent e){

try{

JTree tree=(JTree)e.getSource();

int rowLocation=tree.getRowForLocation(e.getX(),e.getY());

TreePath treepath=tree.getPathForRow(rowLocation);

TreeNode treenode=(TreeNode)treepath.getLastPathComponent();

nodeName=treenode.toString();

}catch(NullPointerException ne){}

}

}

}

用JAVA編寫(xiě)鏈表類(lèi),要求編寫(xiě)能夠從頭部添加節(jié)點(diǎn)。

public?class?ZLinkedList?{

private?int?size;

private?Node?head;

public?ZLinkedList(){

size?=?0;

}

public?void?headInsert(Object?obj){

//if(null==?obj)?{//?do?something}

Node?temp?=?new?Node(obj);

if(size?==0){

head?=?temp;

}else{

temp.setNext(head);

head?=?temp;

}

size++;

}

public?void?preOrder(){

int?length?=?size;

Node?temp?=?head;

for(int?i=?0;i??length;i?++){

System.out.println(temp.getValue());

temp?=?temp.getNext();

}

}

private?static?class?Node{

private?Object?value;

private?Node?next;

Node(){

}

Node(Object?val){

this.value?=?val;

}

public?Object?getValue()?{

return?value;

}

public?void?setValue(Object?value)?{

this.value?=?value;

}

public?Node?getNext()?{

return?next;

}

public?void?setNext(Node?next)?{

this.next?=?next;

}

}

public?static?void?main(String[]?args)?{

ZLinkedList?test?=?new?ZLinkedList();

test.headInsert("1");

test.headInsert("2");

test.headInsert("3");

test.preOrder();

}

}

用JAVA編寫(xiě)這個(gè)鏈表類(lèi),要求能夠從尾部添加節(jié)點(diǎn),詳細(xì)代碼。

public?class?ZLinkedList?{

private?int?size;

private?Node?head;

public?ZLinkedList(){

size?=?0;

}

public?void?headInsert(Object?obj){

//if(null==?obj)?{//?do?something}

Node?temp?=?new?Node(obj);

if(size?==0){

head?=?temp;

}else{

temp.setNext(head);

head?=?temp;

}

size++;

}

public?void?tailInsert(Object?obj){

//?if(null==?obj)?{//?do?something}

Node?temp?=?new?Node(obj);

if?(size?==?0)?{

head?=?temp;

}?else?{

Node?t?=?head;

while(t.getNext()!=null)

t=?t.getNext();

t.setNext(temp);

}

size++;

}

public?void?preOrder(){

int?length?=?size;

Node?temp?=?head;

for(int?i=?0;i??length;i?++){

System.out.println(temp.getValue());

temp?=?temp.getNext();

}

}

private?static?class?Node{

private?Object?value;

private?Node?next;

Node(){

}

Node(Object?val){

this.value?=?val;

}

public?Object?getValue()?{

return?value;

}

public?void?setValue(Object?value)?{

this.value?=?value;

}

public?Node?getNext()?{

return?next;

}

public?void?setNext(Node?next)?{

this.next?=?next;

}

}

public?static?void?main(String[]?args)?{

ZLinkedList?test?=?new?ZLinkedList();

test.tailInsert("1");

test.tailInsert("2");

test.tailInsert("3");

test.preOrder();

}

}

//java?qq交流群:251832769

用JAVA語(yǔ)言這以下程序代碼。 鏈表類(lèi) 1.能夠從頭部添加節(jié)點(diǎn)。 2.能夠從尾部添加節(jié)點(diǎn)。 3.

package com.llist;

public class SingleLinkListT implements LListT {

protected NodeT head; // 頭指針,指向單鏈表的頭結(jié)點(diǎn)

// 默認(rèn)構(gòu)造方法,構(gòu)造空單鏈表

public SingleLinkList(){

head = new NodeT();

}

// 由指定數(shù)組中的多個(gè)對(duì)象構(gòu)造單鏈表,采用尾查構(gòu)造單鏈表

public SingleLinkList(T[] element){

this();

NodeT rear = this.head;

for(int i=0;ielement.length;i++){

rear.next = new NodeT(element[i],null);

rear = rear.next;

}

}

// 判斷鏈表是否為空

public boolean isEmpty() {

return this.head.next==null;

}

// 返回鏈表的長(zhǎng)度

public int length() {

int count = 0;

NodeT p = this.head.next;

while(p!=null){

count++;

p = p.next;

}

return count;

}

// 返回第i(i0)個(gè)元素,若i的指定序號(hào)無(wú)效,則返回null

public T get(int i) {

if(i=0||ithis.length()){

return null;

}

else{

NodeT p = this.head;

for(int j=0;ji;j++){

p = p.next;

}

return p.data;

}

}

// 設(shè)置第i(i0)個(gè)元素的值為x,若i指定序號(hào)無(wú)效,則拋出序號(hào)越界異常

public void set(int i, T x) {

if(x==null)

return;

if(i=0||ithis.length()){

// 拋出序號(hào)越界異常

throw new IndexOutOfBoundsException("指定序號(hào)i="+i+"越界!");

}

else{

NodeT p = this.head;

for(int j=0;ji;j++){

p = p.next;

}

p.data = x;

}

}

// 鏈表的插入操作

public void insert(int i, T x) {

if(x==null) // 插入對(duì)象不能為空

return;

if(i==0){ // 插在頭結(jié)點(diǎn)之后

head.next = new NodeT(x,this.head.next);

}

else if(i0i=this.length()){

NodeT p = this.head;

// 尋找插入位置

for(int j=0;ji;j++){

p = p.next;

}

// 插入x作為p結(jié)點(diǎn)的后繼結(jié)點(diǎn)

p.next = new NodeT(x,p.next);

}

else{

// 拋出序號(hào)越界異常

throw new IndexOutOfBoundsException("指定序號(hào)i="+i+"越界!");

}

}

// 在單鏈表的最后添加對(duì)象

public void append(T x) {

insert(this.length(),x);

}

// 刪除序號(hào)為i的結(jié)點(diǎn),操作成功返回給對(duì)象,否則返回null

public T remove(int i) {

if(i=0||ithis.length()){

return null;

}

else{

NodeT p = this.head;

// 定位到待刪除結(jié)點(diǎn)(i)的前驅(qū)結(jié)點(diǎn)(i-1)

for(int j=0;ji-1;j++){

p = p.next;

}

T old = p.next.data;

p.next = p.next.next;

return old;

}

}

// 刪除單鏈表的所有元素

public void removeAll() {

this.head.next = null;

}

// 查找,返回首次出現(xiàn)關(guān)鍵子key的元素的序號(hào)

public int Search(T key) {

if(key==null)

return 0;

else{

int i = 0;

NodeT p = this.head;

while(p.next!=null){

i++;

p = p.next;

if(p.data.equals(key))

break;

}

if(p.next==null)

return 0;

else

return i;

}

}

//返回鏈表的所有元素的描述字符串,覆蓋Object類(lèi)的toString()方法

public String toString(){

String str = " ( ";

NodeT p = this.head.next;

while(p!=null){

str += p.data.toString();

p = p.next;

if(p!=null)

str += ",";

}

str += " ) ";

return str;

}

}

新聞標(biāo)題:java列表新加節(jié)點(diǎn)代碼 java新建一個(gè)列表
瀏覽路徑:http://muchs.cn/article14/dosjsge.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供手機(jī)網(wǎng)站建設(shè)、網(wǎng)站改版、網(wǎng)站策劃、移動(dòng)網(wǎng)站建設(shè)、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

營(yíng)銷(xiāo)型網(wǎng)站建設(shè)