欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java對象數(shù)組的添加、刪除和遍歷代碼示例

 更新時間:2024年04月01日 10:00:25   作者:2301_80250542  
在Java編程中,我們經(jīng)常需要對數(shù)據(jù)結(jié)構(gòu)進(jìn)行遍歷操作,并根據(jù)業(yè)務(wù)需求刪除部分元素,這篇文章主要給大家介紹了關(guān)于Java對象數(shù)組的添加、刪除和遍歷的相關(guān)資料,需要的朋友可以參考下

前言

創(chuàng)建一個Student的類,屬性包含學(xué)號,姓名,年齡 ,在此基礎(chǔ)上進(jìn)行對象的添加,刪除,遍歷

null調(diào)用方法必定報錯,所以要判斷數(shù)組里的元素(本題數(shù)組里的每個元素都是一個對象)是否為null

類 

package text;

public class Student {
    private String id;
    private String name;
    private int age;

    public Student() {
    }

    public Student(String id, String name, int age) {
        this.id = id;
        this.name = name;
        this.age = age;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
    private static boolean contains(Student[] arr,Student s){
        boolean flag=true;
        for (int i = 0; i < arr.length; i++) {

            if(arr[i]!=null){  //因?yàn)閚ull調(diào)用其他的方法會報錯
                if(arr[i].getId()==s.getId()){
                    //相同,id重復(fù)
                    flag=false;
                }
            }
            //為空的話直接進(jìn)行下個索引的判斷
        }
        return flag;
    }
    private static int indexCount(Student[] arr){
        int count=0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                count++;
            }
        }
        return count;
    }
    private static Student[] CreatNewArr(Student[] arr){
        Student[] newArr=new Student[arr.length+1];
        for (int i = 0; i < arr.length; i++) {
            newArr[i]=arr[i];
        }
        return newArr;
    }
    private static int judgeIndex(Student[] arr){
        int index=0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]==null){
                index=i;
                break;
            }
        }
        return index;
    }
}

對象的創(chuàng)建與使用(添加,遍歷)

題目:創(chuàng)建一個含3個對象的數(shù)組,然后在添加一個對象在數(shù)組當(dāng)中,最后遍歷

分析:

1,創(chuàng)建一個長度為3的數(shù)組

2,創(chuàng)建學(xué)生對象

3,把學(xué)生對象添加到數(shù)組當(dāng)中

4,再次創(chuàng)建一個學(xué)生對象,添加到數(shù)組當(dāng)中

5,判斷id的唯一性,來確定是否添加對象

6,添加對象,判斷原數(shù)組是否存滿。未滿:直接添加;滿了:創(chuàng)建新數(shù)組

package text;

public class StudentTest {
    public static void main(String[] args) {

        //1,創(chuàng)建一個長度為3的數(shù)組
        Student[] arr=new Student[3];
        //2,創(chuàng)建學(xué)生對象
        Student s1=new Student("001","肘子",19);
        Student s2=new Student("002","花木蘭",120);
        Student s3=new Student("003","頭牌",9);
        //3,把學(xué)生對象添加到數(shù)組當(dāng)中
        arr[0]=s1;
        arr[1]=s2;
        arr[2]=s3;
        //4,再次創(chuàng)建一個學(xué)生對象,添加到數(shù)組當(dāng)中
        Student s4=new Student("004","西施",18);
        //5,判斷id的唯一性
        boolean flag=contains(arr,s4);
        //6,添加對象
        if(flag){
            //6.2id沒有重復(fù)-----添加學(xué)生對象;
            //判斷當(dāng)前數(shù)組是否滿了,沒有滿,直接添加;滿了,創(chuàng)建新數(shù)組去添加
            int count=indexCount(arr);
            if(count==arr.length){
                //數(shù)組滿了,創(chuàng)建新數(shù)組
                System.out.println("數(shù)組滿了,創(chuàng)建新數(shù)組newArr[]");
                Student[] newArr=CreatNewArr(arr);
                newArr[newArr.length-1]=s4;
                //遍歷添加后的數(shù)組
                for (int i = 0; i < newArr.length; i++) {
                    System.out.println(newArr[i].getId()+","+newArr[i].getName()+","+newArr[i].getAge());
                }
            }else{
                //數(shù)組沒有滿了,查找數(shù)組的空位置,再存儲進(jìn)去
                System.out.println("數(shù)組沒有滿了,查找數(shù)組的空位置,再存儲進(jìn)arr[]");
                int index=judgeIndex(arr);
                arr[index]=s4;
                //遍歷添加后的數(shù)組
                for (int i = 0; i < arr.length; i++) {
                    System.out.println(arr[i].getId()+","+arr[i].getName()+","+arr[i].getAge());
                }
            }

        }else{
            //6.1id重復(fù)-----提示重復(fù),重新輸入
            System.out.println("id重復(fù),重新輸入");
        }


    }


    private static boolean contains(Student[] arr,Student s){
        boolean flag=true;
        for (int i = 0; i < arr.length; i++) {

            if(arr[i]!=null){  //因?yàn)閚ull調(diào)用其他的方法會報錯
                if(arr[i].getId()==s.getId()){
                    //相同,id重復(fù)
                    flag=false;
                }
            }
            //為空的話直接進(jìn)行下個索引的判斷
        }
        return flag;
    }
    private static int indexCount(Student[] arr){
        int count=0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                count++;
            }
        }
        return count;
    }
    private static Student[] CreatNewArr(Student[] arr){
        Student[] newArr=new Student[arr.length+1];
        for (int i = 0; i < arr.length; i++) {
            newArr[i]=arr[i];
        }
        return newArr;
    }
    private static int judgeIndex(Student[] arr){
        int index=0;
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]==null){
                index=i;
                break;
            }
        }
        return index;
    }
}

結(jié)果

注意 

對象的創(chuàng)建與使用 (刪除)

題目:通過id刪除學(xué)生信息

分析:判斷id在數(shù)組中存不存在。存在,則刪除;不存在,則提示刪除失敗

找到id對應(yīng)的索引,將對應(yīng)的數(shù)組元素置為null

package text;
import java.util.Scanner;
public class deleteStudnet {
    public static void main(String[] args) {
        //1,創(chuàng)建一個長度為3的數(shù)組
        Student[] arr=new Student[3];
        //2,創(chuàng)建學(xué)生對象
        Student s1=new Student(1,"肘子",19);
        Student s2=new Student(2,"花木蘭",120);
        Student s3=new Student(3,"頭牌",9);
        //3,把學(xué)生對象添加到數(shù)組當(dāng)中
        arr[0]=s1;
        arr[1]=s2;
        arr[2]=s3;
        System.out.println("原數(shù)組:");
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                System.out.println(arr[i].getId()+","+arr[i].getName()+","+arr[i].getAge());
            }
        }
        //4,刪除指定id的對象----找到該id對應(yīng)的索引
        System.out.println("請輸入要刪除的id");
        Scanner sc=new Scanner(System.in);
        int id=sc.nextInt();
        int index=discoverIndex(arr,id);//查找該id對應(yīng)的數(shù)組
        if(index>=0){
            arr[index]=null;
        }else{
            System.out.println("刪除失敗");
        }

        //5,遍歷數(shù)組
        System.out.println("刪除后的數(shù)組:");
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                System.out.println(arr[i].getId()+","+arr[i].getName()+","+arr[i].getAge());
            }
        }
    }
    private static int discoverIndex(Student[] arr,int id){

        for (int i = 0; i < arr.length; i++) {
            if(arr[i]!=null){
                if(arr[i].getId()==id){

                    return i;

                }
            }
        }
        return -1;//因?yàn)椴淮嬖?1的索引
    }
}

結(jié)果

附:Java根據(jù)姓名刪除對象數(shù)組中的元素

可以使用Java中的ArrayList類來實(shí)現(xiàn)根據(jù)姓名刪除對象數(shù)組中的元素,具體實(shí)現(xiàn)步驟如下:

  • 創(chuàng)建一個ArrayList對象,將對象數(shù)組中的元素添加到該ArrayList中。
  • 遍歷ArrayList,找到需要刪除的元素的索引。
  • 使用ArrayList.remove()方法刪除指定索引的元素。
  • 將ArrayList轉(zhuǎn)換為數(shù)組并返回。

代碼示例:

import java.util.ArrayList;

public class RemoveObjByName {
    public static void main([string](https://geek.csdn.net/edu/8802d631b97a4a6af1f4d0bbf8527465?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&amp;spm=1055.2569.3001.10083)[] args) {
        // 創(chuàng)建對象數(shù)組
        Person[] personArr = {
                new Person("Alice", 18),
                new Person("Bob", 20),
                new Person("Charlie", 22)
        };

        // 將對象數(shù)組轉(zhuǎn)換為ArrayList
        ArrayList<Person> personList = new ArrayList<>();
        for (Person person : personArr) {
            personList.add(person);
        }

        // 根據(jù)姓名刪除元素
        [string](https://geek.csdn.net/edu/8802d631b97a4a6af1f4d0bbf8527465?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&amp;spm=1055.2569.3001.10083) name = "Bob";
        for (int i = 0; i < personList.size(); i++) {
            if (personList.get(i).getName().equals(name)) {
                personList.remove(i);
                break;
            }
        }

        // 將ArrayList轉(zhuǎn)換為數(shù)組并返回
        Person[] newPersonArr = personList.toArray(new Person[personList.size()]);
        for (Person person : newPersonArr) {
            System.out.println(person.getName() + ", " + person.getAge());
        }
    }
}

class Person {
    private [string](https://geek.csdn.net/edu/8802d631b97a4a6af1f4d0bbf8527465?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&amp;spm=1055.2569.3001.10083) name;
    private int age;

    public Person([string](https://geek.csdn.net/edu/8802d631b97a4a6af1f4d0bbf8527465?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&amp;spm=1055.2569.3001.10083) name, int age) {
        this.name = name;
        this.age = age;
    }

    public [string](https://geek.csdn.net/edu/8802d631b97a4a6af1f4d0bbf8527465?dp_token=eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6NDQ0MDg2MiwiZXhwIjoxNzA3MzcxOTM4LCJpYXQiOjE3MDY3NjcxMzgsInVzZXJuYW1lIjoid2VpeGluXzY4NjQ1NjQ1In0.RrTYEnMNYPC7AQdoij4SBb0kKEgHoyvF-bZOG2eGQvc&amp;spm=1055.2569.3001.10083) getName() {
        return name;
    }

    public int getAge() {
        return age;
    }
}

總結(jié) 

到此這篇關(guān)于Java對象數(shù)組的添加、刪除和遍歷的文章就介紹到這了,更多相關(guān)Java對象數(shù)組添加、刪除和遍歷內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論