C SHARP With Visual Studio Use this array: [] pets = {\"dog\", \"cat\", \"parake
ID: 3726007 • Letter: C
Question
C SHARP With Visual Studio
Use this array:
[] pets = {"dog", "cat", "parakeet", "guinea pig", "dire rabbit", "hamster", "ferret", "velociraptor"};
Using a for loop go through the array
Change the value of each element by adding an s to the end
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
Answer these questions in the code using comments
Can you do this?
If not, why not?
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) – Do not hard code the values, do it programmatically
Explanation / Answer
pet at index 2 is parakeets§ e.g. when counter = 2 I should see §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
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
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.