Java array, remove duplicate values, add, delete array elements of the implementation method _java

Source: Internet
Author: User

The

looks like this:

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 ();
		//Remove duplicate values in array public static void Testa () {String [] str = {"Java", "C + +", "Php", "C #", "Python", "C + +", "Java"};
		for (String elementa:str) {System.out.print (Elementa + "");
		} list<string> List = new arraylist<string> ();
			for (int i=0; i<str.length; i++) {if (!list.contains (str[i))) {List.add (str[i]);
		}/* Set<string> Set = new hashset<string> ();
		for (int i=0; i<str.length; i++) {set.add (str[i]); 
		} string[] Newstr = Set.toarray (new string[1));
		* * SYSTEM.OUT.PRINTLN (); string[] Newstr = List.toarray (new string[1)); Returns an array of the specified types containing all objects for (String elementb:newstr) {System.out.print (ELEMENTB +" ");
	} System.out.println ();
		//Delete One of the elements in the array public static void Testb () {String [] str = {"Java", "C + +", "Php", "C #", "Python"};
		for (String elementa:str) {System.out.print (Elementa + "");
		}//delete php list<string> List = new arraylist<string> ();
		for (int i=0; i<str.length; i++) {list.add (str[i]); } list.remove (2);
		List.remove ("Php") System.out.println (); string[] Newstr = List.toarray (new string[1));
		Returns an array of the specified types for all objects (String elementb:newstr) {System.out.print (Elementb + "");
	} System.out.println ();
		Add an element to the array public static void Testc () {String [] str = {"Java", "C + +", "Php", "C #", "Python"};
		for (String elementa:str) {System.out.print (Elementa + "");
		}//Add ruby list<string> List = new arraylist<string> ();
		for (int i=0; i<str.length; i++) {list.add (str[i]); } list.add (2, "Ruby");
		List.add ("Ruby") System.out.println (); string[] Newstr = List.toarray (New STRING[1]);
		Returns an array of the specified types for all objects (String elementb:newstr) {System.out.print (Elementb + "");
	} System.out.println (); }
}

Console results:


The above Java array, remove duplicate values, add, delete the array elements of the implementation method is small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.