Skip to content

Instantly share code, notes, and snippets.

@johobemax
Created July 29, 2010 04:14
Show Gist options
  • Save johobemax/497213 to your computer and use it in GitHub Desktop.
Save johobemax/497213 to your computer and use it in GitHub Desktop.
public class KahenSample
{
public static void main(String... args)
{
search(5,10,2,4,5,7,9);
}
public static void search(int key, int... nums)
{
boolean hit = false;
for(int i=0; i<nums.length; i++){
if(key == nums[i]){
hit = true;
System.out.println((i+1) + "番目に見つかりました");
break;
}
}
if(!hit){
System.out.println("見つかりませんでした");
}
}
}
class Seito
{
private int kumi;
private String name;
Seito(int _kumi, String _name)
{
kumi = _kumi;
name = _name;
}
public int getKumi()
{
return kumi;
}
public String getName()
{
return name;
}
}
public class KahenSample2
{
public static void main(String[] args)
{
Seito s1 = new Seito(1,"Yamada");
Seito s2 = new Seito(2,"Tanaka");
Seito s3 = new Seito(1,"Yoshida");
Seito s4 = new Seito(2,"Ueda");
Seito s5 = new Seito(1,"Teshima");
ichikumi(s1,s2,s3,s4,s5);
}
/**
* 一組の学生の名前を表示するメソッド、
* ichikumi(Seito... s)を作成せよ。
*/
public static void ichikumi(Seito... s)
{
for(int i=0; i<s.length; i++){
if(s[i].getKumi() == 1){
System.out.println(s[i].getName());
}
}
}
/** 拡張for文を用いた場合
public static void ichikumi(Seito... s)
{
for(Seito ss:s){
if(ss.getKumi() == 1){
System.out.println(ss.getName());
}
}
}
*/
}
@johobemax
Copy link
Author

可変長引数を使ったメソッドのサンプルプログラム

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment