对于基本数据类型和常见的抽象数据类型不做考虑
针对的是由程序编写者创建的抽象数据类型
通过查看API文档,提供了一个remove(Object O)方法
**
**
import java.util.*;
/**
*
*/
public class Main {
public static class Node{
int num;
String val;
private Node(int num, String val){
this.num = num;
this.val = val;
}
}
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
//LinkedList list = new LinkedList<>();
list.add(new Node(5,"aa"));
list.add(new Node(6,"aa"));
list.add(new Node(7,"aa"));
list.add(new Node(8,"aa"));
list.add(new Node(9,"aa"));
list.add(new Node(10,"aa"));
list.add(new Node(11,"aa"));
list.add(new Node(5,"aa"));
for(Node c:list){
System.out.print(c.num + c.val +" ");
}
System.out.println();
for(int i = 0; i < list.size(); i++){
if(list.get(i).num == 5 && list.get(i).val.equals("aa")){
list.remove(i);
}
}
for(Node c: list){
System.out.print(c.num + c.val + " ");
}
}
}
结果:删除了num== 5,val == “aa”的所有节点
其实这个是不对的,有个细节没有考虑到,测试一下下面的数据就知道了
list.add(new Node(5,"aa"));
list.add(new Node(5,"aa"));
list.add(new Node(6,"aa"));
list.add(new Node(7,"aa"));
list.add(new Node(8,"aa"));
list.add(new Node(9,"aa"));
list.add(new Node(10,"aa"));
list.add(new Node(10,"aa"));
list.add(new Node(11,"aa"));
for(int i = 0; i < list.size(); i++){
if(list.get(i).num == 5 && list.get(i).val.equals("aa")){
list.remove(i);
/**修改的部分*/
i--;
}
}
增加更该节点数据的功能,需要在节点类里加入get(),set()方法;
import java.util.*;
/**
*
*/
public class Main {
public static class Node{
int num;
String val;
private Node(int num, String val){
this.num = num;
this.val = val;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getVal() {
return val;
}
public void setVal(String val) {
this.val = val;
}
}
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
list.add(new Node(5,"aa"));
list.add(new Node(6,"aa"));
list.add(new Node(7,"aa"));
list.add(new Node(8,"aa"));
list.add(new Node(9,"aa"));
list.add(new Node(10,"aa"));
list.add(new Node(11,"aa"));
list.add(new Node(5,"aa"));
for(Node c:list){
System.out.print(c.num + c.val +" ");
}
System.out.println();
/**
* 更改地方
*/
for(int i = 0; i < list.size(); i++){
if(list.get(i).num == 5 && list.get(i).val.equals("aa")){
list.remove(i);
/**更改的地方**/
i--;
/**更改地方**/
continue;
}
if(list.get(i).num == 10 && list.get(i).val.equals("aa")){
list.get(i).setVal("uu");
}
}
for(Node c: list){
System.out.print(c.num + c.val + " ");
}
}
}
list.remove(Object o);
import java.util.*;
/**
*
*/
public class Main {
public static class Node{
int num;
String val;
private Node(int num, String val){
this.num = num;
this.val = val;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getVal() {
return val;
}
public void setVal(String val) {
this.val = val;
}
}
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
Node node1 = new Node(5,"aa");
Node node2 = new Node(6,"bb");
Node node3 = new Node(7,"cc");
Node node4 = new Node(8,"cc");
Node node5 = new Node(9,"aa1");
Node node6 = new Node(10,"cc");
list.add(node1);
list.add(node2);
list.add(node3);
list.add(node4);
list.add(node5);
list.add(node6);
list.add(node1);
for(Node c:list){
System.out.print(c.num + c.val +" ");
}
System.out.println();
/**更改地方*/
list.remove(node1);
for (Node node0 : list){
System.out.print(node0.num + node0.val + " ");
}
}
}
**
**
import java.util.ArrayList;
/**
*
*/
public class Main {
public static class Node{
private int num;
private String val;
public Node(int num, String val){
this.num = num;
this.val = val;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getVal() {
return val;
}
public void setVal(String val) {
this.val = val;
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(obj == null){
return false;
}
if(obj instanceof Node){
Node node = (Node) obj;
if(node.num == this.num && node.val.equals(this.val)){
return true;
}
return false;
}
return false;
}
}
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
list.add(new Node(5,"aa"));
list.add(new Node(6,"aa"));
list.add(new Node(7,"aa"));
list.add(new Node(8,"aa"));
list.add(new Node(9,"aa"));
list.add(new Node(10,"aa"));
list.add(new Node(5,"aa"));
for(Node c:list){
System.out.print(c.num + c.val +" ");
}
System.out.println();
Node delete = new Node(5,"aa");
for (int i = 0; i < list.size(); i++){
if(delete.equals(list.get(i))) {
list.remove(i);
i--;
}
}
for (Node node0 : list){
System.out.print(node0.num + node0.val + " ");
}
System.out.println();
}
}
**
import java.util.ArrayList;
/**
*
*/
public class Main {
public static class Node{
private int num;
private String val;
public Node(int num, String val){
this.num = num;
this.val = val;
}
// public int getNum() {
// return num;
// }
//
// public void setNum(int num) {
// this.num = num;
// }
//
// public String getVal() {
// return val;
// }
//
// public void setVal(String val) {
// this.val = val;
// }
//
// @Override
// public int hashCode() {
// return super.hashCode();
// }
//
// @Override
// public boolean equals(Object obj) {
// if(this == obj) return true;
// if(obj == null){
// return false;
// }
// if(obj instanceof Node){
// Node node = (Node) obj;
// if(node.num == this.num && node.val.equals(this.val)){
// return true;
// }
// return false;
// }
// return false;
// }
}
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
list.add(new Node(5,"aa"));
list.add(new Node(6,"aa"));
list.add(new Node(7,"aa"));
list.add(new Node(8,"aa"));
list.add(new Node(9,"aa"));
list.add(new Node(10,"aa"));
list.add(new Node(5,"aa"));
for(Node c:list){
System.out.print(c.num + c.val +" ");
}
System.out.println();
Node delete = new Node(5,"aa");
list.remove(delete);
// for (int i = 0; i < list.size(); i++){
// if(delete.equals(list.get(i))) {
// list.remove(i);
// i--;
// }
// }
for (Node node0 : list){
System.out.print(node0.num + node0.val + " ");
}
System.out.println();
}
}
重写equal();
import java.util.ArrayList;
/**
*
*/
public class Main {
public static class Node{
private int num;
private String val;
public Node(int num, String val){
this.num = num;
this.val = val;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getVal() {
return val;
}
public void setVal(String val) {
this.val = val;
}
@Override
public int hashCode() {
return super.hashCode();
}
@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(obj == null){
return false;
}
if(obj instanceof Node){
Node node = (Node) obj;
if(node.num == this.num && node.val.equals(this.val)){
return true;
}
return false;
}
return false;
}
}
public static void main(String[] args) {
ArrayList list = new ArrayList<>();
list.add(new Node(5,"aa"));
list.add(new Node(6,"aa"));
list.add(new Node(7,"aa"));
list.add(new Node(8,"aa"));
list.add(new Node(9,"aa"));
list.add(new Node(10,"aa"));
list.add(new Node(5,"aa"));
for(Node c:list){
System.out.print(c.num + c.val +" ");
}
System.out.println();
Node delete = new Node(5,"aa");
list.remove(delete);
// for (int i = 0; i < list.size(); i++){
// if(delete.equals(list.get(i))) {
// list.remove(i);
// i--;
// }
// }
for (Node node0 : list){
System.out.print(node0.num + node0.val + " ");
}
System.out.println();
}
}