Java arrays: deduplication, add, delete elements

Source: Internet
Author: User

Importjava.util.List;Importjava.util.ArrayList;ImportJava.util.Set;ImportJava.util.HashSet; Public classCode { Public Static voidmain (String [] args) {TestA (); System.out.println ("===========================");        Testb (); System.out.println ("===========================");    TESTC (); }    //remove duplicate values from the array     Public Static voidTestA () {String [] str= {"Java", "C + +", "Php", "C #", "Python", "C + +", "Java"};  for(String elementa:str) {System.out.print (Elementa+ " "); } List<String> list =NewArraylist<string>();  for(inti=0; i<str.length; i++) {            if(!list.contains (Str[i]))            {List.add (str[i]); }        }string[] Newstr= List.toarray (NewSTRING[1]);//returns an array containing the specified type of all objects         for(String elementb:newstr) {System.out.print (Elementb+ " ");    } System.out.println (); }    //Delete One of the elements in an array     Public Static voidTestb () {String [] str= {"Java", "C + +", "Php", "C #", "Python"};//Remove PHPlist<string> list =NewArraylist<string>();  for(inti=0; i<str.length; i++) {List.add (str[i]); } list.remove (2);//list.remove ("Php")string[] Newstr= List.toarray (NewSTRING[1]);//returns an array containing the specified type of all objects    }    //add an element to an array     Public Static voidTESTC () {String [] str= {"Java", "C + +", "Php", "C #", "Python"};//Add Rubylist<string> list =NewArraylist<string>();  for(inti=0; i<str.length; i++) {List.add (str[i]); } list.add (2, "Ruby");string[] Newstr= List.toarray (NewSTRING[1]);//returns an array containing the specified type of all objects    }}

Java arrays: deduplication, add, delete elements

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.