Java数组,去掉重复值、增加、删除数组元素的方法

网友投稿 202 2023-07-02


Java数组,去掉重复值、增加、删除数组元素的方法

如下所示:

import java.util.List;

import java.util.ArrayList;

import java.util.Set;

import java.util.HashSet;

public class lzwCode {

public static void main(String [] args) {

testA();

System.out.println("===========================");

testB();

System.out.println("===========================");

testC();

}

//去掉数组中重复的值

public static void testA() {

String [] str = {"Java", "C++", "php", "C#", "python", "C++", "Java"};

for (String elementA:str ) {

System.out.print(elementA + " ");

}

List list = new ArrayList();

for (int i=0; i

if(!list.contains(str[i])) {

list.add(str[i]);

}

}

/*

Set set = new HashSet();

for (int i=0; i

set.add(str[i]);

}

String[] newStr = set.toArray(new String[1]);

*/

System.out.println();

String[] newStr = list.toArray(new String[1]); //返回一个包含所有对象的指定类型的数组

for (String elementB:newStr ) {

System.out.print(elementB + " ");

}

System.out.println();

}

//删除数组中其中一个元素eVOQaSgYWa

public static void testB() {

String [] str = {"Java", "C++", "Php", "C#", "Python"};

for (String elementA:str ) {

System.out.print(elementA + " ");

}

//删除php

List list = new ArrayList();

for (int i=0; i

list.add(str[i]);

}

list.remove(2); //list.remove("Php")

System.out.println();

String[] newStr = list.toArray(new String[1]); //返回一个包含所有对象的指定类型的数组

for (String elementB:newStr ) {

System.out.print(elementB + " ");

}

System.out.println();

}

//在数组中增加一个元素

public static void testC() {

String [] str = {"Java", "C++", "Php", "C#", "Python"};

for (String elementA:str ) {

System.out.print(elementA + " ");

}

//增加ruby

List list = new ArrayList();

for (int i=0; i

list.add(str[i]);

}

list.add(2, "ruby"); //list.add("ruby")

System.out.println();

String[] newStr = list.toArray(new String[1]); //返回一个包含所有对象的指定类型的数组

for (String elementB:newStr ) {

System.out.print(elementB + " ");

}

System.out.println();

}

}

控制台结果:

if(!list.contains(str[i])) {

list.add(str[i]);

}

}

/*

Set set = new HashSet();

for (int i=0; i

set.add(str[i]);

}

String[] newStr = set.toArray(new String[1]);

*/

System.out.println();

String[] newStr = list.toArray(new String[1]); //返回一个包含所有对象的指定类型的数组

for (String elementB:newStr ) {

System.out.print(elementB + " ");

}

System.out.println();

}

//删除数组中其中一个元素eVOQaSgYWa

public static void testB() {

String [] str = {"Java", "C++", "Php", "C#", "Python"};

for (String elementA:str ) {

System.out.print(elementA + " ");

}

//删除php

List list = new ArrayList();

for (int i=0; i

list.add(str[i]);

}

list.remove(2); //list.remove("Php")

System.out.println();

String[] newStr = list.toArray(new String[1]); //返回一个包含所有对象的指定类型的数组

for (String elementB:newStr ) {

System.out.print(elementB + " ");

}

System.out.println();

}

//在数组中增加一个元素

public static void testC() {

String [] str = {"Java", "C++", "Php", "C#", "Python"};

for (String elementA:str ) {

System.out.print(elementA + " ");

}

//增加ruby

List list = new ArrayList();

for (int i=0; i

list.add(str[i]);

}

list.add(2, "ruby"); //list.add("ruby")

System.out.println();

String[] newStr = list.toArray(new String[1]); //返回一个包含所有对象的指定类型的数组

for (String elementB:newStr ) {

System.out.print(elementB + " ");

}

System.out.println();

}

}

控制台结果:

set.add(str[i]);

}

String[] newStr = set.toArray(new String[1]);

*/

System.out.println();

String[] newStr = list.toArray(new String[1]); //返回一个包含所有对象的指定类型的数组

for (String elementB:newStr ) {

System.out.print(elementB + " ");

}

System.out.println();

}

//删除数组中其中一个元素eVOQaSgYWa

public static void testB() {

String [] str = {"Java", "C++", "Php", "C#", "Python"};

for (String elementA:str ) {

System.out.print(elementA + " ");

}

//删除php

List list = new ArrayList();

for (int i=0; i

list.add(str[i]);

}

list.remove(2); //list.remove("Php")

System.out.println();

String[] newStr = list.toArray(new String[1]); //返回一个包含所有对象的指定类型的数组

for (String elementB:newStr ) {

System.out.print(elementB + " ");

}

System.out.println();

}

//在数组中增加一个元素

public static void testC() {

String [] str = {"Java", "C++", "Php", "C#", "Python"};

for (String elementA:str ) {

System.out.print(elementA + " ");

}

//增加ruby

List list = new ArrayList();

for (int i=0; i

list.add(str[i]);

}

list.add(2, "ruby"); //list.add("ruby")

System.out.println();

String[] newStr = list.toArray(new String[1]); //返回一个包含所有对象的指定类型的数组

for (String elementB:newStr ) {

System.out.print(elementB + " ");

}

System.out.println();

}

}

控制台结果:

list.add(str[i]);

}

list.remove(2); //list.remove("Php")

System.out.println();

String[] newStr = list.toArray(new String[1]); //返回一个包含所有对象的指定类型的数组

for (String elementB:newStr ) {

System.out.print(elementB + " ");

}

System.out.println();

}

//在数组中增加一个元素

public static void testC() {

String [] str = {"Java", "C++", "Php", "C#", "Python"};

for (String elementA:str ) {

System.out.print(elementA + " ");

}

//增加ruby

List list = new ArrayList();

for (int i=0; i

list.add(str[i]);

}

list.add(2, "ruby"); //list.add("ruby")

System.out.println();

String[] newStr = list.toArray(new String[1]); //返回一个包含所有对象的指定类型的数组

for (String elementB:newStr ) {

System.out.print(elementB + " ");

}

System.out.println();

}

}

控制台结果:

list.add(str[i]);

}

list.add(2, "ruby"); //list.add("ruby")

System.out.println();

String[] newStr = list.toArray(new String[1]); //返回一个包含所有对象的指定类型的数组

for (String elementB:newStr ) {

System.out.print(elementB + " ");

}

System.out.println();

}

}

控制台结果:


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java 对象的序列化和反序列化详细介绍
下一篇:第一次接触Bootstrap框架
相关文章

 发表评论

暂时没有评论,来抢沙发吧~