Deleting Duplicate Element Using HashSet

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.*;
 
public class Main {
 
    public static void main(String args[]){
 
    String[] fav = {"red","red","apple""apple""hallo"};
 
    Set<String> mySet  = new HashSet<String>();
    Collections.addAll(mySet,fav);
 
       System.out.println(mySet);
    }
}
cs

result / 결과

1
[red, apple, hallo]
cs

Java String Instance Practice Example #1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package edu.handong.csee.java.string; 
 
public class FamilyNamePrinter{
    private String[] stringNames; 
    private String[] familyNames;
     
    public void printFamilyNames(){
        for(String name: familyNames){
            if(name == null){
                System.out.print(""); 
            }else{
                System.out.println(name);
            }
        }
    }
    public FamilyNamePrinter(String names){
        
       stringNames = names.split(",");
       familyNames = new String[stringNames.length];
       int i=0
       for(String name: stringNames)
         familyNames[i++= name.trim().split(" ")[1]; 
    }
 
    public static void main (String[] args) {
        String myString = args[0]; 
        FamilyNamePrinter myFamily = new FamilyNamePrinter(myString); 
        myFamily.printFamilyNames();     
    }
}
cs

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import java.util.ArrayList;
 
public class Test {
    public static void main(String[] args) {
        ArrayList<String> pitches = new ArrayList<String>();
        pitches.add("138");
        pitches.add("129");
        pitches.add("142");
 
        System.out.println(pitches.get(1));
        System.out.println(pitches.size());
        System.out.println(pitches.contains("142"));
 
        System.out.println(pitches.remove("129"));
        System.out.println(pitches.size());
        System.out.println(pitches.remove(0));
    }
}
cs

[Java Programming] Java return, 자바 리턴값 배우기


[Person.java]


class Person{

String name = "Lin";

String bloodType;

int age = 20;


void speak(){

System.out.println("저의 이름은"+name+"이고 혈액형은"+bloodType+"입니다.");

}

String getName(){

return name;

}


int getAge(){

return age;

}

}


[Application.java]


public class Application{

public static void main(String[] agrs){

Person person1 = new Person();

String myName = person1.getName();

int myAge = person1.getAge() + 6;

System.out.println(myAge);

}

}


[result]

26

[Java Programming] Java Method 자바 메소드 배우기


class Person{

String name;

String bloodType;


void speak(){

System.out.println("저의 이름은"+name+"이고 혈액형은"+bloodType+"입니다.");

}

}


class Cat{

String name;

}


public class Application{

public static void main(String[] args){

Person person1 = new Person();

person1.name = "Lin";

person1.bloodType = "A";

person1.speak();


Cat cat1 = new Cat();

cat1.name = "Kitty";

}

}


// 같은 package 안에 있으면 클래스 호출 가능.


[result]

저의 이름은 Lin이고 혈액형은 A입니다.

+ Recent posts