Categories
Blog

The How-Tos When It Comes To Defining, Assigning, And Printing A Java Array

How to define, assign, and print an array using Java. šŸ”ƒ

Defining an array (character type) of a size 3 calledĀ array

char [] array = new char[10]

Ask user to enter 10 characters and assign each of the characters into theĀ array

for (int i = 0; i < array.length; i++)
  array[i] = keyboard.next().charAt(0);

Use the data inĀ arrayĀ to reverse a list calledĀ reverseĀ so that the first element in the list calledĀ arrayĀ is the last element inĀ reverse

for (int i = 0; i < array.length; i++)
  reverse[reverse.length ā€“ i] = array[i];

Write the first use way to useĀ for-loopĀ to print out all elements in the list calledĀ reverse

for (int i = 0; i < reverse.length; i++)
  System.out.print(reverse[i] + " " );

Write the second use way to useĀ for-loopĀ to print out all elements in the list calledĀ reverse

for (char value : reverse)
  System.out.print(value + " ");

Define a 3 by 4 floating point values array, calledĀ scores

double [][] scores = new double [3][4];

Assign the value for each cell in the array calledĀ scoresĀ toĀ columnIndex * 20 / rowIndex

for (int row = 0; row < scores.length; row++)
  for (int col = 0; col < scores[row].length; col++)
    scores[row][col] = col * 20 / (double) row;

Print all the values in the array calledĀ scores

for (int row = 0; row < scores.length; row++) {
  for (int col = 0; col < scores[row].length; col++)
    System.out.print (scores[row][col] + "\t");
  System.out.println();
}

Write aĀ findMaxĀ method which finds the maximum value in the data stored inĀ scores

static double findMax(double [][] scores) {
  double max = scores[0][0];
  for (int row = 0; row < scores.length; row++)
    for (int col = 0; col < scores[row].length; col++)
      if (scores[row][col] > max)
        max = myScores[row][col];
  return max;
}

 

By Frances Coronel

Frances Coronel is a software engineer specializing in UI development on the Customer Acquisition Team at Slack where her mission is to make your working life simpler, more pleasant, and more productive.

She has been working professionally as a developer since 2015 and holds a Bachelors in Computer Science from Hampton University and a Masters in Computer Science from Cornell Tech.

Outside of Slack, Frances is an Executive Director of Techqueria, a 501c3 nonprofit that serves the largest community of Latinx in Tech in the US.

She also supports Code Nation as a member of their Bay Area Leadership Council and the Latino Community Foundation as a member of their Latinos in Tech Giving Circle.