Question

In: Computer Science

few problems example of array and 2d array and the solution code in java language. I...

few problems example of array and 2d array and the solution code in java language. I am new to java and trying to learn this chapter and it is kinda hard for me to understand.

Solutions

Expert Solution

Java program for 1d and 2d array :-

public class MyClass {
public static void main(String args[]) {
int arr[] = new int[5]; //creating an array of 5 elements
  
//how to define elements of the array
arr[0]=5;
arr[1]=6;
arr[2]=7;
arr[3]=8;
arr[4]=9;
  
//printing elements of the array
for(int i=0;i<5;i++)
{
System.out.print(arr[i]+" ");
}
  
//creating new 2d aray
int arr_d[][] = new int[2][2]; //creating 2d matrix or 2d array
  
//accessing i and j columns
arr_d[0][0] = 2;
arr_d[0][1] = 3;
arr_d[1][0] = 4;
arr_d[1][1] = 5;
  
  
System.out.println();
System.out.println();
System.out.println("The 2d array :");
//printing 2d matrix
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
System.out.print(arr_d[i][j]+" ");
}
  
System.out.println();
}
  
}
}

Output:-


Related Solutions

show some example of few recursive problem in java language and the solution code. Need more...
show some example of few recursive problem in java language and the solution code. Need more practice as a new learner
Show some examples of a few Parameters and Objects problems in java language and the solution...
Show some examples of a few Parameters and Objects problems in java language and the solution code. Need more practice as a new learner
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow Example: Score 1 score 2 Player1 20 21 Player2 15 32 Player3 6 7 Using the method ScoreIterator so that it returns anonymous object of type ScoreIterator , iterate over all the scores, one by one. Use the next() and hasNext() public interface ScoreIterator { int next(); boolean hasNext(); Class ScoreBoard : import java.util.*; public class ScoreBoard { int[][] scores ; public ScoreBoard...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow Example: Score 1 score 2 Player1 20 21 Player2 15 32 Player3 6 7 Using the method ScoreIterator so that it returns anonymous object of type ScoreIterator , iterate over all the scores, one by one. Use the next() and hasNext() public interface ScoreIterator { int next(); boolean hasNext(); Class ScoreBoard : import java.util.*; public class ScoreBoard { int[][] scores ; public ScoreBoard...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde...
In java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow Example: Score 1 score 2 Player1 20 21 Player2 15 32 Player3 6 7 Using the method ScoreIterator so that it returns anonymous object of type ScoreIterator , iterate over all the scores, one by one. Use the next() and hasNext() public interface ScoreIterator { int next(); boolean hasNext(); Class ScoreBoard : import java.util.*; public class ScoreBoard { int[][] scores ; public ScoreBoard...
Java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow...
Java. I have class ScoreBoard that holds a 2d array of each player's score. COde Bellow Example: Score 1 score 2 Player1 20 21 Player2 15 32 Player3 6 7 Using the method ScoreIterator so that it returns anonymous object of type ScoreIterator , iterate over all the scores, one by one. Use the next() and hasNext() public interface ScoreIterator { int next(); boolean hasNext(); Class ScoreBoard : import java.util.*; public class ScoreBoard { int[][] scores ; public ScoreBoard (int...
This is my C language code. I have some problems with the linked list. I can't...
This is my C language code. I have some problems with the linked list. I can't store the current. After current = temp, I don't know how to move to the next node. current = current-> next keeps making current into NULL. #include #include #include #include struct node{int data; struct node *next;}; int main() {     struct node *head, *current, *temp, *trash;     srand(time(0));     int randNumber = rand()%51;     if(randNumber != 49)     {         temp = (struct node*)malloc(sizeof(struct node));         current = (struct node*)malloc(sizeof(struct node));...
In java language how would I write the following? Create an array called “boyNames” and store...
In java language how would I write the following? Create an array called “boyNames” and store all names from the BoyNames.txt file Similarly, create an array called “girlsNames” and store all names from the GirlNames.txt file Create a text menu that allowing users to:          1. Enter a name and the application must display a message indicating if the name is   among the most popular (found on either array) If the name is found, tell the user if it is a boy’s...
Write a java program of a multiplication table of binary numbers using a 2D array of...
Write a java program of a multiplication table of binary numbers using a 2D array of integers.
The programming language that is being used here is JAVA, below I have my code that...
The programming language that is being used here is JAVA, below I have my code that is supposed to fulfill the TO-DO's of each segment. This code in particular has not passed 3 specific tests. Below the code, the tests that failed will be written in bold with what was expected and what was outputted. Please correct the mistakes that I seem to be making if you can. Thank you kindly. OverView: For this project, you will develop a game...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT