189 8069 5689

java顺序表的删除代码 顺序表的删除怎么实现的

数据结构,用JAVA编写顺序表

直接用数组或者ArrayList实现相应的功能,行吗?Java没有指针,LinkedList无法模拟

成都创新互联10多年成都企业网站定制服务;为您提供网站建设,网站制作,网页设计及高端网站定制服务,成都企业网站定制及推广,对成都墙体彩绘等多个领域拥有丰富的网站营销经验的网站建设公司。

java如何实现对数据表里面的数据删除(最好给个具体代码范例)

连接数据库

public class DBManager {

//定义数据库连接的URL

private static final String URL="jdbc:sqlserver://localhost:1433;database=j1105";

//定义数据库的用户名

private static final String USERNAME = "sa";

//定义数据库密码

private static final String PASSWORD = "sa";

//定义一个连接的引用,使用单例模式

private static Connection conn = null;

//使用静态块来注册驱动

//类加载时自动执行代码块

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;

}

/**

* 关闭的一些操作 , 优化

* @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个参数的情况下关闭

* @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);

}

实现

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语言建立含有若干个元素的顺序表,并实现插入,删除,查找等基本操作

java 中的List接口就是顺序存储的集合机构,底层是用数组实现的,检索性能高,插入和删除性能较低,因为涉及到移位。代码简单演示:

ListInteger list = new ArrayListInteger(); // 定义一个用于存放整数的集合list,

list.add(100);

list.add(200);

list.add(300); // 将100,200,300 一次加入到list中

System.out.println(list.toString()); // 查看结果

int a = list.get(0) ; // 这将找出list中的第一个元素100,赋值给a

System.out.println(a); // 100

list.remove(2); // 删除list中的第三个元素

System.out.println(list.toString()); // 查看结果

------------------------------------------------------------------------------------------------------

比较粗略,详细内容请查看ArrayList 的 API 。祝你学习进步。

java使用顺序表制作电子通讯录,实现添加,删除和排序的功能.

翻了半天找到以前写的,只写了部分,你可以参考一下

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("学号 姓名 分数:");

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--结束");

int choose = sc.nextInt();

switch (choose){

case 1:

System.out.print("插入位置:");

int n = sc.nextInt();

Data data = new Data();

System.out.print("学号 姓名 分数:");

data.setStuNumber(sc.nextLong());

data.setStuName(sc.next());

data.setStuScore(sc.nextLong());

sq.insertData(n,data);

break;

case 2:

System.out.print("打印结果: ");

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("元素内容是: ");

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;

}

}

用Java语言编写数据结构中顺序表的插入删除查找代码并实现

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怎么实现表格的行删除

java中表格的删除是通过事件监控来实现的,示例代码如下:

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"}}; //数据

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; //行数加上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顺序表的删除代码 顺序表的删除怎么实现的
当前URL:http://jkwzsj.com/article/hiocic.html

其他资讯