开发者

Java: how to create 10 cats? when Cat ("cat" + i) = "cat name" not working as in other languages

using loop, I can create

My cat is: Cat1
...
My cat is: Cat1

However, when I tried to use

  Cat ("cat"+i) = new Cat("Cat" + i);

I'm making mistakes....

So, what is the simplist way to correct my code to produce

cat1 ... cat10 cat instances?


public class TestCat{ 
  public static void main(String [] args){ 

 for (int i=1; i<10; i++){     
   //Cat ("cat"+i) = new Cat("Cat 1");
   Cat cat1 = new Cat("Cat 1");  
   System.out.println("My cat is:  " + cat1 ); 
  }
 } 
} 

class Cat{

 static String catName;
 public Cat(String catName){
   this.catName=catName;
 }
 public String toString(){
   return catName;
}
}

Sorry....I should say

How to create ten 10 Cat instances.....cat1, ...cat2.....because开发者_Go百科 in other languages, I can use "cat"||i = ..., to create different varaibles, I wonder how I could do similar things in Java....

In other words, I want to name the instances I'm going to create by taking the loop information into account.


Use a collection if you don't know how many cats you'll have.

public class TestCat
{ 
  public static void main(String [] args)
  { 

    Cat[] cats = new Cat[10];    
    Vector catsUnlimited = new Vector(10);    
    Cat myCat = null;

    for (int i=1; i<11; i++)
    {     
    myCat = new Cat("Cat" + i); 
      cats[i-1]= myCat; 

      catsUnlimited.addElement(new Cat("Cat" + i));

      System.out.println("My cat is:  " + cats[i-1] ); 
    }

    System.out.println("Known cats");
    for (int x = 0; x < catsUnlimited.size(); x++)
    {        
     System.out.println("Cat #" + (x+1) + ":" +(Cat)catsUnlimited.get(x));
    }    
  } 
} 

Note: This code produces incorrect results and had me stumped until I looked closer at the Cat class.

My cat is:  Cat1
My cat is:  Cat2
My cat is:  Cat3
My cat is:  Cat4
My cat is:  Cat5
My cat is:  Cat6
My cat is:  Cat7
My cat is:  Cat8
My cat is:  Cat9
My cat is:  Cat10
Known cats
Cat #1:Cat10
Cat #2:Cat10
Cat #3:Cat10
Cat #4:Cat10
Cat #5:Cat10
Cat #6:Cat10
Cat #7:Cat10
Cat #8:Cat10
Cat #9:Cat10
Cat #10:Cat10

Here was the culprit:

public class Cat
{
     static String catName;

Remove the static and you are golden.


This looks like an academic problem, so I'll approach it as such. You need to store ten cats somewhere. One place you can do that is in an array..

public class TestCat{ 
  public static void main(String [] args){ 

    Cat[] cats = new Cat[10];

    // Create my cats
    for (int i= 0; i < 10; i++) {     
      cats[i] = new Cat("Cat " + i + 1);  
    }

    // Print them out
    for (Cat aCat : cats) {
      System.out.printLn("My Cat is: " + aCat);
    }
  } 
} 


for (int i=1; i<10; i++){     
   Cat cat = new Cat("Cat" + i);  
   System.out.println("My cat is:  " + cat ); 
}


public class TestCat
{ 
  public static void main(String [] args)
  { 

    Cat[] cats = new Cat[10];
    for (int i=1; i<11; i++)
    {     
      cats[i-1]= new Cat("Cat" + i);

      System.out.println("My cat is:  " + cats[i-1] ); 
    }
  } 
} 


Lots of answers above so I'll just add that you can't dynamically change variable names in Java, so the bolded part below is a nono:

Cat ("cat"+i) = new Cat("Cat 1");


You're hardcoding the cat number:

Cat cat1 = new Cat("Cat 1"); 

You should change that to:

Cat cat1 = new Cat("Cat " + i); 

Right?


You need an array.

  String[] cats = new String[10];

  for(int i = 0; i < cats.length; i++){
     cats[i] = "Cat" + i;
  }

This creates and stores the 10 cats.

Then printing is similar:

  for(int i = 0; i < cats.length; i++){
     System.out.println("My cat is " + cats[i]);
  }


You can do:

for (int i=1; i<10; i++){
        Cat cat = new Cat("Cat"+i);
        System.out.println("My cat is:  " + cat );
}


Java does not support dynamically named variables, so you can't create "cat1" through "cat10" with:

Cat ("cat"+i) = new Cat("Cat" + i);

(BTW, what language are you coming from?)

The Java way would be to create an array (as others have suggested). If you don't know how many instances you will need, you can dynamically size the array:

public class TestCat
{
    static class Cat
    {
        private String name;

        Cat(String name)
        {
            this.name = name;
        }

        @Override
        public String toString()
        {
            return "Cat{" +
                    "name='" + name + '\'' +
                    '}';
        }
    }
    public static void main(String[] args)
    {
        Cat[] someCats = createCats(10);
        printCats(someCats);

        Cat[] lotsaCats = createCats(42);
        printCats(lotsaCats);
    }

    private static void printCats(Cat[] cats)
    {
        // Print them out
        for (Cat aCat : cats)
        {
            System.out.println("My Cat is: " + aCat);
        }
    }

    private static Cat[] createCats(int ncats)
    {
        Cat[] cats = new Cat[ncats];

        // Create my cats
        for (int i = 0; i < ncats; i++)
        {
            cats[i] = new Cat("Cat " + i + 1);
        }
        return cats;
    }
} 

You could also use a List instead of an array.


You can create n number of Cats :

Code:

public class TestCat
{

  public static void main(String [] args) throws IOException
  {  
      int x=0; 
      DataInputStream in=new DataInputStream(System.in);
      x=Integer.parseInt(in.readLine());
      Cat[] cats = new Cat[x];
      for (int i=1; i<x; i++)
      {     
          cats[i-1]= new Cat("Cat" + i);       
          System.out.println("My cat is:  " + cats[i-1] ); 
      }
   }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜