直接用數(shù)組或者ArrayList實現(xiàn)相應(yīng)的功能,行嗎?Java沒有指針,LinkedList無法模擬
成都創(chuàng)新互聯(lián)10多年成都企業(yè)網(wǎng)站定制服務(wù);為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計及高端網(wǎng)站定制服務(wù),成都企業(yè)網(wǎng)站定制及推廣,對成都墻體彩繪等多個領(lǐng)域擁有豐富的網(wǎng)站營銷經(jīng)驗的網(wǎng)站建設(shè)公司。
連接數(shù)據(jù)庫
public class DBManager {
//定義數(shù)據(jù)庫連接的URL
private static final String URL="jdbc:sqlserver://localhost:1433;database=j1105";
//定義數(shù)據(jù)庫的用戶名
private static final String USERNAME = "sa";
//定義數(shù)據(jù)庫密碼
private static final String PASSWORD = "sa";
//定義一個連接的引用,使用單例模式
private static Connection conn = null;
//使用靜態(tài)塊來注冊驅(qū)動
//類加載時自動執(zhí)行代碼塊
static {
//反射com.microsoft.sqlserver.jdbc.SQLServerDriver.class
try {
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//獲得連接
//在程序使用過程中始終只有1個對象存在
//使用單例模式來給Connection賦值
public static Connection getConnection(){
if(conn == null){
try {
conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
} catch (SQLException e) {
e.printStackTrace();
}
}
return conn;
}
/**
* 關(guān)閉的一些操作 , 優(yōu)化
* @param conn
* @param stat
* @param rs
*/
public static void close(Connection conn,Statement stat,ResultSet rs){
try{
if(conn != null){
conn.close();
}
if(stat != null){
stat.close();
}
if(rs != null){
rs.close();
}
}catch(SQLException e){
e.printStackTrace();
}
}
/**
* 重寫上面的方法,在只有2個參數(shù)的情況下關(guān)閉
* @param conn
* @param stat
*/
public static void close(Connection conn,Statement stat){
try{
if(conn != null){
conn.close();
}
if(stat != null){
stat.close();
}
}catch(SQLException e){
e.printStackTrace();
}
}
public static void main(String[] args){
Connection conn = DBManager .getConnection();
System.out.println(conn);
}
}
接口
public interface IStudentDao {
public void deleteStudent(int xh);
}
實現(xiàn)
public class StudentDAOimpl implements IStudentDao {
public void deleteStudent(int xh) {
try{
String sql = "delete from tb_student where xh = ?";
PreparedStatement ps = conn.prepareStatement(sql);
ps.setInt(1, xh);
ps.executeUpdate();
System.out.println("成功刪除");
}catch(SQLException e){
e.printStackTrace();
}
}
}
java 中的List接口就是順序存儲的集合機構(gòu),底層是用數(shù)組實現(xiàn)的,檢索性能高,插入和刪除性能較低,因為涉及到移位。代碼簡單演示:
ListInteger list = new ArrayListInteger(); // 定義一個用于存放整數(shù)的集合list,
list.add(100);
list.add(200);
list.add(300); // 將100,200,300 一次加入到list中
System.out.println(list.toString()); // 查看結(jié)果
int a = list.get(0) ; // 這將找出list中的第一個元素100,賦值給a
System.out.println(a); // 100
list.remove(2); // 刪除list中的第三個元素
System.out.println(list.toString()); // 查看結(jié)果
------------------------------------------------------------------------------------------------------
比較粗略,詳細內(nèi)容請查看ArrayList 的 API 。祝你學(xué)習(xí)進步。
翻了半天找到以前寫的,只寫了部分,你可以參考一下
public class Data {
private long stuNumber;
private String stuName;
private long stuScore;
public Data(){}
public Data(long stuNumber,String stuName,long stuScore){
this.stuNumber=stuNumber;
this.stuName=stuName;
this.stuScore=stuScore;
}
public void setStuNumber(long stuNumber){this.stuNumber=stuNumber;}
public void setStuName(String stuName){this.stuName=stuName;}
public void setStuScore(long stuScore){this.stuScore=stuScore;}
public long getStuNumber(){return stuNumber;}
public String getStuName(){return stuName;}
public long getStuScore(){return stuScore;}
}
public interface LinkListIntf {
public void initList();//初始化
public int size();//獲得長度
public void insertData(int n,Data listdata);
public void getData(int n);
public void deleteData(int n);
public void printList();
}
public class ListTest {
public static void main(String[] args) {
int i;
Sqlist sq = new Sqlist();
Data pdata;
System.out.println("--------Start----------");
System.out.println("--------Init------------");
sq.initList();
System.out.println("--------Init Over--------");
Scanner sc = new Scanner(System.in);
System.out.println("輸入元素:");
for (int m = 0; m 2; m++) {
Data data = new Data();
System.out.print("學(xué)號 姓名 分數(shù):");
data.setStuNumber(sc.nextLong());
data.setStuName(sc.next());
data.setStuScore(sc.nextLong());
sq.insertData(sq.size()+1, data);
}
while (true) {
System.out.println("----請選擇操作------");
System.out.print("1--插入;2--打印;3--刪除;4--查找;5--結(jié)束");
int choose = sc.nextInt();
switch (choose){
case 1:
System.out.print("插入位置:");
int n = sc.nextInt();
Data data = new Data();
System.out.print("學(xué)號 姓名 分數(shù):");
data.setStuNumber(sc.nextLong());
data.setStuName(sc.next());
data.setStuScore(sc.nextLong());
sq.insertData(n,data);
break;
case 2:
System.out.print("打印結(jié)果: ");
sq.printList();
break;
case 3:
System.out.print("請輸入要刪除的元素的位置: ");
int deleteLocation = sc.nextInt();
sq.deleteData(deleteLocation);
break;
case 4:
System.out.print("請輸入要查詢的元素的位置: ");
int findLocation = sc.nextInt();
System.out.print("元素內(nèi)容是: ");
sq.getData(findLocation);
break;
case 5:
return;
}
}
}
}
public class Sqlist implements LinkListIntf{
public static final int MAX_LEN = 100;
public int listLength;
private Data[] listdata = new Data[MAX_LEN];
//初始化順序表
public void initList(){this.listLength=0;}
public int size(){return this.listLength;}
public void insertData(int n,Data data){
int i;
if(listLengthMAX_LEN)
System.out.println("表滿!");
if(n1 || nlistLength+1)
System.out.println("插入位置越界!");
for(i=listLength-1;i=n-1;i--)
listdata[i+1]=listdata[i];
listdata[n-1]=data;
listLength++;
return;
}
public void deleteData(int n){
if(n1 || nlistLength-1){System.out.println("刪除位置不正確!");return;}
for(int i=n;ilistLength;i++)
listdata[i-1]=listdata[i];
listLength--;
System.out.println("----刪除成功!-----");
return;
}
public void getData(int n){
if(n1 || nlistLength){System.out.println("查找不正確!");}
System.out.println(listdata[n-1].getStuNumber()+" "+listdata[n-1].getStuName()+" "+listdata[n-1].getStuScore());
//return 0;
}
public void printList(){
int i;
System.out.println("表中信息為:");
for(i=0;ilistLength;i++) {
String format = String.format(listdata[i].getStuName()+" "+listdata[i].getStuNumber()+" "+listdata[i].getStuScore());
System.out.println(format);
}
//return 0;
}
}
public class Test {
public static void main(String[] args) {
int length = 5;
int ai = 1;
String data = "data";
String[] array = insertArrar(data, ai, length);
data = delArray(array, ai, length);
System.out.println(data);
}
public static String[] insertArrar(String data,int ai,int length){
String[] array = new String[length];
array[ai] = data;
return array;
}
public static String delArray(String[] array,int ai,int length){
String data = "";
data=array[ai];
array[ai]=null;
for(int i = 0; iarray.length;i++){
System.out.println(array[i]);
}
return data;
}
}
java中表格的刪除是通過事件監(jiān)控來實現(xiàn)的,示例代碼如下:
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.table.DefaultTableModel;
//維護表格
public class JTableDefaultTableModelTest extends JFrame{
private DefaultTableModel tableModel; //表格模型對象
private JTable table;
private JTextField aTextField;
private JTextField bTextField;
public JTableDefaultTableModelTest()
{
super();
setTitle("表格");
setBounds(100,100,500,400);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
String[] columnNames = {"A","B"}; //列名
String [][]tableVales={{"A1","B1"},{"A2","B2"},{"A3","B3"},{"A4","B4"},{"A5","B5"}}; //數(shù)據(jù)
tableModel = new DefaultTableModel(tableVales,columnNames);
table = new JTable(tableModel);
JScrollPane scrollPane = new JScrollPane(table); //支持滾動
getContentPane().add(scrollPane,BorderLayout.CENTER);
//jdk1.6
//排序:
//table.setRowSorter(new TableRowSorter(tableModel));
table.setSelectionMode(ListSelectionModel.SINGLE_SELECTION); //單選
table.addMouseListener(new MouseAdapter(){ //鼠標事件
public void mouseClicked(MouseEvent e){
int selectedRow = table.getSelectedRow(); //獲得選中行索引
Object oa = tableModel.getValueAt(selectedRow, 0);
Object ob = tableModel.getValueAt(selectedRow, 1);
aTextField.setText(oa.toString()); //給文本框賦值
bTextField.setText(ob.toString());
}
});
scrollPane.setViewportView(table);
final JPanel panel = new JPanel();
getContentPane().add(panel,BorderLayout.SOUTH);
panel.add(new JLabel("A: "));
aTextField = new JTextField("A4",10);
panel.add(aTextField);
panel.add(new JLabel("B: "));
bTextField = new JTextField("B4",10);
panel.add(bTextField);
final JButton addButton = new JButton("添加"); //添加按鈕
addButton.addActionListener(new ActionListener(){//添加事件
public void actionPerformed(ActionEvent e){
String []rowValues = {aTextField.getText(),bTextField.getText()};
tableModel.addRow(rowValues); //添加一行
int rowCount = table.getRowCount() +1; //行數(shù)加上1
aTextField.setText("A"+rowCount);
bTextField.setText("B"+rowCount);
}
});
panel.add(addButton);
final JButton updateButton = new JButton("修改"); //修改按鈕
updateButton.addActionListener(new ActionListener(){//添加事件
public void actionPerformed(ActionEvent e){
int selectedRow = table.getSelectedRow();//獲得選中行的索引
if(selectedRow!= -1) //是否存在選中行
{
//修改指定的值:
tableModel.setValueAt(aTextField.getText(), selectedRow, 0);
tableModel.setValueAt(bTextField.getText(), selectedRow, 1);
//table.setValueAt(arg0, arg1, arg2)
}
}
});
panel.add(updateButton);
final JButton delButton = new JButton("刪除");
delButton.addActionListener(new ActionListener(){//添加事件
public void actionPerformed(ActionEvent e){
int selectedRow = table.getSelectedRow();//獲得選中行的索引
if(selectedRow!=-1) //存在選中行
{
tableModel.removeRow(selectedRow); //刪除行
}
}
});
panel.add(delButton);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
JTableDefaultTableModelTest jTableDefaultTableModelTest = new JTableDefaultTableModelTest();
jTableDefaultTableModelTest.setVisible(true);
}
}
當前標題:java順序表的刪除代碼 順序表的刪除怎么實現(xiàn)的
標題路徑:http://vcdvsql.cn/article2/hiocic.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站營銷、網(wǎng)站收錄、外貿(mào)建站、靜態(tài)網(wǎng)站、網(wǎng)站導(dǎo)航、ChatGPT
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)