Win a copy of Mastering Corda: Blockchain for Java Developers this week in the Cloud/Virtualization forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Bear Bibeault
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Jj Roberts
  • Carey Brown
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

arrays

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class number
{
public int[][] array2D = {
{ 1, 2, 3 ,4 , 5},
{ 6, 7, 8, 9, 10},
{ 11, 12, 13, 14, 15},
{ 16, 17, 18, 19, 20}
};
{
int a= Console.readInt("add a number");
array2D[0][0]=a;
int b= Console.readInt("add a number");
array2D[0][1]=b;
int c= Console.readInt("add a number");
array2D[0][2]=c;
int d= Console.readInt("add a number");
array2D[0][3]=d;
int e= Console.readInt("add a number");
array2D[0][4]=e;
int f= Console.readInt("add a number");
array2D[1][0]=f;
int g= Console.readInt("add a number");
array2D[1][1]=g;
int h= Console.readInt("add a number");
array2D[1][2]=h;
....etc

My question is:Is there a way to shorten this or do I have to to do this
for every component of the array?
 
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mandy fern:
public class number
{
public int[][] array2D = {
{ 1, 2, 3 ,4 , 5},
{ 6, 7, 8, 9, 10},
{ 11, 12, 13, 14, 15},
{ 16, 17, 18, 19, 20}
};
{
int a= Console.readInt("add a number");
array2D[0][0]=a;
int b= Console.readInt("add a number");
array2D[0][1]=b;
int c= Console.readInt("add a number");
array2D[0][2]=c;
int d= Console.readInt("add a number");
array2D[0][3]=d;
int e= Console.readInt("add a number");
array2D[0][4]=e;
int f= Console.readInt("add a number");
array2D[1][0]=f;
int g= Console.readInt("add a number");
array2D[1][1]=g;
int h= Console.readInt("add a number");
array2D[1][2]=h;
....etc

My question is:Is there a way to shorten this or do I have to to do this
for every component of the array?


Instead of reading each number into an int and then moving it into an element of your array, couldn't you read a number from console directly into the arrary element. That way you could put this in a loop and use a variable in each of the two elements of your array and just increment them each time you add another number.
I think this would work.
 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code goes here:
for(int i=0;i<5;i++)
for(in j=0;j<5;j++)
array2d[i][j]=Console.readInt("Enter a number");
Thanks.
[This message has been edited by Bindesh Vijayan (edited September 01, 2001).]
 
mandy fern
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks a lot!
 
Wink, wink, nudge, nudge, say no more, it's a tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic