• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

Creating a 2d square matrix from a 2d integer arraylist

 
Ranch Hand
Posts: 477
Eclipse IDE Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Folks,

Ive written the following code that converts a 2d arraylist into a 2d matrix, but now I need to check that the matrix created is also square(num of rows = num of columns).

Could anyone explain how I could possibly modify my code to acheive that?


My code is
 
Marshal
Posts: 75874
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What’s a 2D list?     Is there such a thing? I think there isn’t, only a List of Lists.
What’s a 2D array? Is there such a thing? I think there isn’t, only an array of arrays.

That may appear pedantic, but arrays of arrays are better than 2D arrays. For example, the outermost array has a length field, which you doubtless already know about. And the element arrays also have a length field. So you can iterate the outer array with a for‑each loop and check that all the lengths are the same.
You can do the same sort of thing with the size() method of your Lists. It might be quicker to do that validation at as early a stage as possible.
 
Campbell Ritchie
Marshal
Posts: 75874
361
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please look for methods which can turn Lists into arrays. You might find them in the java.util.Collections class.
 
pie. 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