Question

• Use this array: string[] pets = {"dog", "cat", "parakeet", "guinea pig", "dire rabbit", "hamster", "ferret",...

• Use this array: string[] pets = {"dog", "cat", "parakeet", "guinea pig", "dire rabbit", "hamster", "ferret", "velociraptor"};

• Using a for loop go through the array o Change the value of each element by adding an s to the end o Print out the index and the animal at that index  e.g. when counter = 2 I should see  pet at index 2 is parakeets

• Using the foreach loop, print out the items of the array • Using the foreach loop change the name of each animal ( by adding “-eats” to the end of each animal o Can you do this? o Why not? o What is the system’s response?

• Create a list that have the same values as the pets array (just create: if you want you can display the items – your choice)

Homework Answers

Answer #1

• Using a for loop go through the array o Change the value of each element by adding an s to the end o Print out the index and the animal at that index  e.g. when counter = 2 I should see  pet at index 2 is parakeets

Code:

ArraysDemo.java

public class ArraysDemo {

   public static void main(String[] args) {
       String[] pets = {"dog", "cat", "parakeet", "guinea pig", "dire rabbit", "hamster", "ferret", "velociraptor"};
      
       for(int i=0;i<pets.length;i++)
       {
           pets[i]=pets[i]+"s";
       }
      
       for(int i=0;i<pets.length;i++)
       {
           System.out.println("Pet at index "+i+" is "+pets[i]);
}

   }

}

________________

Output:

Pet at index 0 is dogs
Pet at index 1 is cats
Pet at index 2 is parakeets
Pet at index 3 is guinea pigs
Pet at index 4 is dire rabbits
Pet at index 5 is hamsters
Pet at index 6 is ferrets
Pet at index 7 is velociraptors

_________________

• Using the foreach loop, print out the items of the array • Using the foreach loop change the name of each animal ( by adding “-eats” to the end of each animal o Can you do this? o Why not? o What is the system’s response?

public class ArraysDemo {

  
   public static void main(String[] args) {

String[] pets = {"dog", "cat", "parakeet", "guinea pig", "dire rabbit", "hamster", "ferret", "velociraptor"};
//Displaying the elements in the array using for-each
       for(String str:pets){
           System.out.println(str);
           }

System.out.println();   

//Here we trying to change the array elements using for-each
       for(String str:pets){
           str=str+"-eats";
             
           }

//Displaying array elements using for-each
       for(String str:pets){
           System.out.println(str);
             
           }
      

   }

}

________________

Output:

cats
parakeets
guinea pigs
dire rabbits
hamsters
ferrets

velociraptors
dogs
cats
parakeets
guinea pigs
dire rabbits
hamsters
ferrets
velociraptors

Here we are trying to modify the elements of the array using for each but we could'nt able to modify,because we not actually changing the elements in the array.Just we are modifying the String value ,which couldnt change array elements.

_______________

• Create a list that have the same values as the pets array (just create: if you want you can display the items – your choice)

public class ArraysDemo {

  
   public static void main(String[] args) {
       String[] pets = {"dog", "cat", "parakeet", "guinea pig", "dire rabbit", "hamster", "ferret", "velociraptor"};
         

//Creating a List class object
       List li=new ArrayList<String>();
      

//Adding the array elements to the List
       for(int i=0;i<pets.length;i++)
       {
           li.add(pets[i]);
       }
      
       Iterator it=li.iterator();

//Displaying the elements in the List
       while(it.hasNext())
       {
           System.out.println(it.next());
       }

   }

}

___________

output:

dogs
cats
parakeets
guinea pigs
dire rabbits
hamsters
ferrets
velociraptors

_______________Thank You

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT