Java 2D arrays 🚚

Поділитися
Вставка
  • Опубліковано 14 жов 2024

КОМЕНТАРІ • 182

  • @BroCodez
    @BroCodez  4 роки тому +136

    public class Main {
    public static void main(String[] args) {

    // 2D array = an array of arrays

    String[][] cars = {
    {"Camaro","Corvette","Silverado"},
    {"Mustang","Ranger","F-150"},
    {"Ferrari","Lambo","Tesla"}
    };

    /*
    cars[0][0] = "Camaro";
    cars[0][1] = "Corvette";
    cars[0][2] = "Silverado";
    cars[1][0] = "Mustang";
    cars[1][1] = "Ranger";
    cars[1][2] = "F-150";
    cars[2][0] = "Ferrari";
    cars[2][1] = "Lambo";
    cars[2][2] = "Tesla";
    */

    for(int i=0; i

  • @RestedAura2
    @RestedAura2 Рік тому +127

    To clarify those who don't understand what does cars.length and cars[i].length means, basically:
    cars.length: detect how many instances/objects are there in the row of the 2D array
    and
    cars[i].length: detects how many instances/objects are there in the column of the 2D array

    • @misfire32
      @misfire32 Рік тому +3

      Thanks bro, the loop now makes sense to me, I didn't understand how the j int was smaller than the i int while they were both zero.

    • @hussinmomen5256
      @hussinmomen5256 10 місяців тому +1

      thx bro

    • @maximizer174
      @maximizer174 5 місяців тому +1

      @RestedAura2 you mean the opposite right?
      cars.length : no of objects in column (vertically/ up to down) or Basically determine no of rows
      cars[i].length : no of objects in row (horizontally/ left to right) or Basically determine no of columns
      i was super confused on this topic. correct me if I'm wrong

    • @transfertransfer986
      @transfertransfer986 5 місяців тому

      ​@@maximizer174bruh, you got it mixed up. Columns are verticles (top to bottom)
      Rows are horizontal (left to right)

    • @maximizer174
      @maximizer174 5 місяців тому

      @@transfertransfer986 isn't that what i said? 😅

  • @JuliHoffman
    @JuliHoffman 2 роки тому +31

    I can't believe I understood this! You have excellent teaching skills.

  • @dolemerchant69
    @dolemerchant69 11 місяців тому +5

    have been watching ur vids the last few weeks since starting CS in college. they’ve helped so much, thank you

  • @pavelkvasnicka6856
    @pavelkvasnicka6856 Рік тому +7

    This is the best Java tutorial for beginners, so you can learn Java and English in one hit. Please keep going! I vote for Java advance tutorial. Thanks a lot Bro

  • @erneztoyo
    @erneztoyo 3 роки тому +183

    System.out.println("Great Video");

    • @Sssamaa-c8w
      @Sssamaa-c8w Рік тому +16

      Boolean comment = true;

    • @godcomplex1929
      @godcomplex1929 10 місяців тому +5

      If(comment = true)
      {System.out.println("Amen to that");}

    • @christiangalagar9213
      @christiangalagar9213 5 місяців тому

      ​@@godcomplex1929error

    • @lobitu3423
      @lobitu3423 3 місяці тому +4

      ​@@godcomplex1929 your comment is messed up

  • @IceBeam93
    @IceBeam93 3 роки тому +15

    I did this with pokemon:
    public class Array2Ds {
    public static void main(String[] args) {
    String[][] pokemon = {{"Bulbasaur", "Ivysaur", "Venusaur"},
    {"Charmander", "Charmeleon", "Charizard"},
    {"Squirtle", "Wartortle", "Blastoise"}
    };
    //This also works but define it at the top already: String[][] pokemon = new String[3][3];
    // pokemon[0][0] = "Bulbasaur";
    // pokemon[0][1] = "Ivysaur";
    // pokemon[0][2] = "Venusaur";
    // pokemon[1][0] = "Charmander";
    // pokemon[1][1] = "Charmeleon";
    // pokemon[1][2] = "Charizard";
    // pokemon[2][0] = "Squirtle";
    // pokemon[2][1] = "Wartortle";
    // pokemon[2][2] = "Blastoise";
    for (int i = 0; i < pokemon.length; i++) {
    System.out.println();
    for (int j = 0; j < pokemon.length; j++) {
    System.out.print(pokemon[i][j]+ " ");
    }
    }
    }
    }

    • @BroCodez
      @BroCodez  3 роки тому +3

      nice! That's a good visualization

    • @d_36_priyanshuurwate65
      @d_36_priyanshuurwate65 2 роки тому +3

      i love pokemon too
      my code is little diffrent but thNK U BRO

    • @Micharl-w6i
      @Micharl-w6i Рік тому

      love aesthetic code@@BroCodez

  • @honoredegg
    @honoredegg 2 роки тому +2

    2d arrays understood completely. 16th. Thank you, ma Bro Sensei!

  • @dillonpaul1498
    @dillonpaul1498 Рік тому +1

    best teacher ever.. good job bro

  • @filip_g
    @filip_g 2 місяці тому +1

    bro!! What is your eclipse theme?? It looks perfect!! Do you know if there is IntelliJ equivalent?

  • @BrokenG-String
    @BrokenG-String 8 місяців тому

    Great vid, although it would have been nice for you to also include how you can access the data within a 2D array and why you would wanna use a 2D array in the first place.

  • @medjl6083
    @medjl6083 3 роки тому +2

    This is important video for multi-dimentional array.

  • @bradleydeans192
    @bradleydeans192 2 роки тому +4

    Why have cars[i].length in the nested for loop? It doesn't seem to be necessary since it will always be the length of the array anyways. Side note: Love these tutorials, they have all been excellent!

    • @udayrajoriyaa
      @udayrajoriyaa Рік тому +8

      Yes it is not necessary if each row has same number of columns as number of rows, i.e. square array.
      Let's say if there are 3 rows total, and each row has 3 columns/elements in them, then cars.length = 3, which will work for this case.
      But, let's say if the array has 3 rows and 2 columns/elements in each row, this logic will break, since again cars.length will be equal to 3, and we know, the third column doesn't exist in any row. So, it's necessary to use cars[i].length as each row is a seperate array in itself.
      Also, each row can have varying number of columns/elements, which makes it extremely necessary to use the logic. For example consider this array:
      Tesla, BMW, Audi, Hyundai
      Mercedes, Volkswagen
      Range Rover, Buggati, Toyota
      First row has 4 elements, second row has 2 elements, while the third row has 3 elements.

    • @bibi.98x
      @bibi.98x Рік тому

      @@udayrajoriyaa thanks for the great explanation!

  • @uc8xo
    @uc8xo Рік тому

    Bro it helped me tommorrow is my exam and was clulessly watching this now

  • @davidbolduc4378
    @davidbolduc4378 3 роки тому

    I love watching these videos before class.

  • @youngdumbguitarist5348
    @youngdumbguitarist5348 Рік тому +1

    This is such a helpful and well explained video! Thanks!

  • @vedmahant7776
    @vedmahant7776 Рік тому +1

    Thanks for the help man, this video helped me a lot.

  • @Clarara-is9li
    @Clarara-is9li 6 місяців тому

    Okay now its etched in my brain Thanks dude :)

  • @leeuwengames315
    @leeuwengames315 18 днів тому

    great explanation just wondering if i want to make an extra row or colum without out replacing the existing ones how do i do that?

  • @mycake1515
    @mycake1515 Рік тому

    When I was watching your 2d array tutorial it reminded me of Inception and the concept of dreams within a dream. Sounds stupid but it actually works lmao.

  • @-omarabusnineh5174
    @-omarabusnineh5174 3 роки тому +1

    Thank u Ms.Bro, I'm from Jordan.

  • @ascar66
    @ascar66 Рік тому

    Good video, thanks for sharing

  • @fieldsfury
    @fieldsfury 2 роки тому

    Amazing video! Watching all of these to refresh and learn!

  • @cesara9747
    @cesara9747 2 роки тому

    Your tutorials are the best!

  • @DesiHabibi
    @DesiHabibi 2 роки тому

    AMAZING ! BEST TUTORIALS HANDS DOWN !

  • @rr3nn638
    @rr3nn638 3 роки тому +5

    Nice quick and easy explanation. Great job!

  • @shyam.upadhyay
    @shyam.upadhyay 2 роки тому +1

    I have smashed that like button so hard, there is a hole is my screen now. Worth it!

  • @danchavyn
    @danchavyn 2 роки тому

    Your video helped me a lot! It was the only tutorial that solved my problem. Thank you!

  • @noah77
    @noah77 4 роки тому +2

    Ooo nice, I wanted one of these.

  • @percivalgebashe4376
    @percivalgebashe4376 Рік тому

    Nice

  • @relaxjam1952
    @relaxjam1952 5 місяців тому

    you are the best

  • @cristiangarciaperez7230
    @cristiangarciaperez7230 2 роки тому +2

    what is cars.length representive of? like does that mean that I increases until all letters of cars are met or until all the cars are met? thanks

    • @albertocasanovalaras3153
      @albertocasanovalaras3153 2 роки тому +3

      cars.length is showing the number of instances that there is on that row. In this case that would be 3. That's why the loop keeps going as long as 'i' is lower than cars.length.
      cars.length = 3
      'i' keeps increasing by 1 until it's no longer less than 3. So it goes like: 0, 1, 2. And then it stops, because 3 !< 3, it's equal.
      The same way, when you type it as cars[i].length, you are getting the number of instances on that column. On this example, that number is still 3, but if you added another row, it would be 4.
      Using this kind of loop, you make sure you're passing through every instance on your array. It's checking the length of every row on the first loop, and the length of every column on the nested loop.
      I hope this made sense, if you have any question, ask again and I'll try to clarify it.

    • @JusticeBeaver619
      @JusticeBeaver619 2 роки тому +1

      @@albertocasanovalaras3153 got it. But can you please explain why we use cars[i].length instead of just cars.length . The value is 3 in both cases

    • @jironamos7650
      @jironamos7650 2 роки тому +1

      @@JusticeBeaver619 Imagine as if he made a variable of Columns and Rows, and assigned the values to each, you could have called that variable in the place of using cars.lenght or cars[i].lenght, which essentially means, "if i is less than Columns" or "if j is less than rows"

  • @emachine003
    @emachine003 Рік тому

    i have a java final Friday, so thanks for this video!

  • @dew6739
    @dew6739 2 роки тому

    Good job

  • @jasonfenstermaker4
    @jasonfenstermaker4 8 місяців тому

    You are getting me through QA class man

  • @witecki_
    @witecki_ 2 роки тому

    bro where chalk up u been, it is so cool

  • @supernovic99
    @supernovic99 Рік тому

    This video was so useful!!! Thank you Bro!

  • @haykmanukyan5462
    @haykmanukyan5462 2 роки тому +1

    simple & great! thanks

  • @loopify3905
    @loopify3905 4 роки тому

    very nice explained

  • @yevgenomelchenko732
    @yevgenomelchenko732 Рік тому

    Thanks for the lesson

  • @jeyhunaliyev1765
    @jeyhunaliyev1765 3 роки тому

    Awesome 👏

  • @soumelee5661
    @soumelee5661 2 роки тому

    cool cars bro

  • @huuloc8719
    @huuloc8719 2 роки тому

    Nice.

  • @coreywoodring3905
    @coreywoodring3905 6 місяців тому

    Great Job!

  • @trickysachin
    @trickysachin 2 роки тому

    Awesome👏👏😊😊

  • @eugenemolchan5141
    @eugenemolchan5141 3 роки тому

    You are the best bro!

  • @justinbanza4751
    @justinbanza4751 3 роки тому

    Thank you very much for this video

  • @曾毓哲-b1t
    @曾毓哲-b1t Рік тому

    thank you very much

  • @zinebjinari7893
    @zinebjinari7893 3 роки тому

    great

  • @hadihiwa5690
    @hadihiwa5690 5 місяців тому

    Thanx bro

  • @ardcodelover
    @ardcodelover Рік тому

    Valuable bro

  • @ibrahimylmaz8378
    @ibrahimylmaz8378 2 роки тому

    you are the man!

  • @MrLoser-ks2xn
    @MrLoser-ks2xn 2 роки тому

    Thanks

  • @shivamgaming3559
    @shivamgaming3559 5 місяців тому

    👍

  • @ByeBaybe
    @ByeBaybe 2 роки тому

    Bro, I literally type this out and I get a 1D aka a list. I've been thinking about this for at least 15 minutes without an answer.
    EDIT: Never mind. I'm a fucking moron. In the last line I wrote sysout + ctrl + space and that's 'System.out.println();' but I need 'System.out.print();'. The 'println' makes a whole new line for each bundle of text forcing the text into a list format. Holy shit I'm happy I'm a stubborn person and figured this out.

  • @GIllies0.3
    @GIllies0.3 2 місяці тому

    very meowtastic

  • @freedom4218
    @freedom4218 3 місяці тому

    Yeah!

  • @mohamedwafik3493
    @mohamedwafik3493 3 роки тому +1

    👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏👏

  • @sihleeundefined1208
    @sihleeundefined1208 3 роки тому

    Thanks bro, keep it up💪🏿💪🏿💪🏿

  • @phiphongnguyen3352
    @phiphongnguyen3352 3 роки тому

    thanks you very much

  • @baharehkjani6761
    @baharehkjani6761 Рік тому

    I kiss your head, dear bro code ! LOVED it ! Thank you !

  • @sairos4057
    @sairos4057 Рік тому

    thanks bro

  • @kirillutsenko9306
    @kirillutsenko9306 2 роки тому

    thx a lot for your help

  • @dianamilenaarchilacordoba4632
    @dianamilenaarchilacordoba4632 Місяць тому

    great video!!! I'm infinitely grateful for your dedication and big heart to share this knowledge with the world. Thank you soo much

  • @zari_723
    @zari_723 2 роки тому

    thanks, it's so helpful

  • @mithunbabbira
    @mithunbabbira 4 роки тому

    amazing

  • @D14R
    @D14R Рік тому

    So
    Public static void main is method
    (String [] args) is array???

  • @bekturasanbekov1979
    @bekturasanbekov1979 Рік тому

    thx 4 vid bro !

  • @PoorwayTraning
    @PoorwayTraning Рік тому

    thanks

  • @jackepra8026
    @jackepra8026 Рік тому

    Thanks you!!
    Very usefull

  • @ma8969
    @ma8969 2 роки тому

    thank you

  • @muhdijas6646
    @muhdijas6646 Місяць тому

    thee macha lub uuuuuuuuuuuuuuuuu

  • @chiefeveryday7541
    @chiefeveryday7541 4 місяці тому

    🐐

  • @nirvomind
    @nirvomind 3 роки тому

    Thanks , best explanation !

  • @Santiago-iu8mk
    @Santiago-iu8mk 2 роки тому

    Gracias brooo

  • @ndivho.k23
    @ndivho.k23 2 місяці тому +1

    what happens when the arrays have to be inserted by the user instead of assigning the arrays

    • @sunlit-nonentity
      @sunlit-nonentity 9 днів тому

      i know this is 2 months old; however, this is such a good question that a lot of beginners often ask and want to know.
      in order to allow a user to create and store their own values in an array, you have to ask yourself one question, “do you want the values stored permanently or temporarily?” meaning, if you want the user to be able to store files in an array they only access while they run their program a single time on your terminal window, then the answer is simple and to code it is even more intuitive and simple.
      HOWEVER, if you want to allow a user to store values in an array that they create, you are diving into the world of Reading and Writing Files !!! this is awesome to learn !!! implementing this is not entirely simple; however, it does not have to be hard. i don’t have enough characters to type all of the code how. however, i hope i have been able to point you in the write direction to go about learning how to do this !!!

  • @kidcracker
    @kidcracker Рік тому

    ty bro

  • @coltonbailey8873
    @coltonbailey8873 3 роки тому +5

    Going on day 5 of your series coming from 0 java experience.
    A day ago my program looked like this.
    import javax.swing.JOptionPane;
    public class Main {
    public static void main(String[] args) {
    String[] toyota = {"97' Celica GT4", "94' MR-2 3SGT", "15' 4-Runner SR5"};
    String[] dodge = {"21' RAM 1500 TRX", "18' Dodge Challenger SRT Demon"};
    String[] subaru = {"Any turbo Subaru. If you can get a turbo Subaru, get a turbo Subaru. Those things are cool!"};
    String make = JOptionPane.showInputDialog("Type in a Vehicle Make to see my opinions on their best versions.");
    while (make.isBlank()) {
    JOptionPane.showMessageDialog(null, "Please type in a Make of vehicle. (Toyota, Dodge, Subaru...");
    break;
    }
    if (make.equalsIgnoreCase("Toyota")) {
    JOptionPane.showMessageDialog(null,toyota);
    }
    if (make.equalsIgnoreCase("Dodge")) {
    JOptionPane.showMessageDialog(null, dodge);
    }
    if (make.equalsIgnoreCase("Subaru")) {
    JOptionPane.showMessageDialog(null, subaru);
    }
    }
    }
    to this~!
    import javax.swing.JOptionPane;
    public class Main {
    public static void main(String[] args) {
    String[][] toyota = new String [2][2];
    String[][] dodge = new String [2][2];
    String[][] subaru = new String [2][2];
    toyota[0][0] = "97' Celica GT4";
    toyota[0][1] = "94' MR2 3SGT";
    toyota[1][0]="15' 4-Runner SR5";
    toyota[1][1]= "20' Camry TRD";
    dodge [0][0] = "21' RAM 1500 TRX";
    dodge [0][1]= "18' Challenger SRT Demon";
    dodge [1][0]= "19' Charger SRT Hellcat";
    dodge [1][1]= "21' Viper SRT";
    subaru[0][0]= "EVERY";
    subaru[0][1]= "SUBARU";
    subaru[1][0]= "IS";
    subaru[1][1]= "GOOD";
    String make = JOptionPane.showInputDialog("Type in a Vehicle Make to see my top 4. Type `all' to view all");
    while (make.isBlank()) {
    JOptionPane.showMessageDialog(null, "Please type in a Make of vehicle, or `all'. (Toyota, Dodge, Subaru...");
    break;
    }
    if (make.equalsIgnoreCase("Toyota")) {
    JOptionPane.showMessageDialog(null,toyota);
    }
    if (make.equalsIgnoreCase("Dodge")) {
    JOptionPane.showMessageDialog(null, dodge);
    }
    if (make.equalsIgnoreCase("Subaru")) {
    JOptionPane.showMessageDialog(null, subaru);
    }
    if (make.equalsIgnoreCase("all")) {

    for (int i=0; i

    • @kidhacker1000
      @kidhacker1000 2 роки тому +1

      Hello, just wondering how far are you into your programming journey now? Its amazing how you were able to do this in 5 days.. May I ask how many hours a day were you studying? Thank you and have a nice day.

  • @Horatius_23
    @Horatius_23 2 роки тому

    THX!

  • @bartomiejsniadach5795
    @bartomiejsniadach5795 2 роки тому

    Thank you Bro

  • @kiki.t2094
    @kiki.t2094 2 роки тому

    Thank you Bro, you gave me an idea how I can solve my code problems 👍🙏

  • @omersond4891
    @omersond4891 3 роки тому

    thanks for this

  • @ghoggaliabdou4222
    @ghoggaliabdou4222 3 роки тому

    Thank you so much 💙💙💙

  • @Simis999
    @Simis999 2 роки тому

    Back to the good microphone

  • @adityaalshi7081
    @adityaalshi7081 2 роки тому

    ❤️👌

  • @aya2222
    @aya2222 3 роки тому

    Why it should be "j

    • @shinkanade1552
      @shinkanade1552 3 роки тому

      I had the same question lol, but i think it's because it's a 2D array.i'm not sur but i think it's to say the length of the row not the column. I will let Bro answer this :p

  • @tamirrozenfeld3572
    @tamirrozenfeld3572 2 роки тому

    Thank!!

  • @عمروأبوالحوف
    @عمروأبوالحوف Рік тому

    how to check if the matrix is quare : matrix[n][n]

  • @greeneggsandmushrooms9855
    @greeneggsandmushrooms9855 3 роки тому

    i miss 3d dorritos

  • @danny.3036
    @danny.3036 3 роки тому

    Thanks, Bro! ☕

  • @uclong1176
    @uclong1176 3 роки тому

    useful

  • @kidhacker1000
    @kidhacker1000 2 роки тому

    I have a question and would really appreciate anyone's help. I'm not understanding what the "length" is supposed to do in i

    • @bryanZ17
      @bryanZ17 2 роки тому +1

      As i undestand, that's used to read the size of the array. So the as long as counter doesn't surpass the size of the array, it will continue to loop around.
      Once, the counter matches the size or 'length' of the array, it will stop.

  • @simon3431
    @simon3431 3 роки тому +2

    hey bro i have a question.. when we use 2D arrays and we should use 2D arrays instead of a normal array and can we use linked list instead of 2D array or matrix ?

    • @perseusgeorgiadis7821
      @perseusgeorgiadis7821 Рік тому

      You use 2D arrays when you want an array of arrays. Technically, you could just make a hashmap or just create separate arrays, or even store the contents of a 2D array into a regular array, but sometimes it’s just a bit cleaner to use 2D arrays, I guess (specifically when representing matrices)

    • @privateonlyar7
      @privateonlyar7 Рік тому

      @@perseusgeorgiadis7821 whats the use arrays you can simply write that is print option

  • @mohamedelfadli3125
    @mohamedelfadli3125 Рік тому

  • @tissue.5706
    @tissue.5706 3 роки тому +2

    Idk whats happening to my Ide but it isnt becoming 2d but instead its a list idk why but i copy pasted ur code and it become 2d then I typed it again and its still a list :T

    • @ByeBaybe
      @ByeBaybe 2 роки тому

      Last line needs to be System.out.print(cars[i][j]+" "); NOT System.out.println(cars[i][j]+" "); . Look for the 'ln' before 'print'. That's causing spaces inbetween each bundle of text.

  • @yeliza2350
    @yeliza2350 2 роки тому

    6 ferbruar l watched

  • @aya2222
    @aya2222 3 роки тому

    Someone help me.... why it should be ( int i=0; i

    • @shinkanade1552
      @shinkanade1552 3 роки тому +2

      @TheFakeViRuZz good answer but it's not complete, i think we need to understand the difference between the "Index (i)" and the "Length (cars.length)".
      When you declare the array of 3 string ( ex : String[ ] cars = new String [3] ) :
      1. The length is 3 ( cars.length, returns 3 )
      2. The index goes from "0" to "2" ( cars[0], cars[1], cars[2] )

    • @aya2222
      @aya2222 3 роки тому +1

      @@shinkanade1552 thanks!!!!!

  • @blueking5739
    @blueking5739 2 роки тому

    I don’t know what’s wrong with my console, it would display everything straight down and not on the side like his. I need help

  • @boxernath
    @boxernath 8 місяців тому

    I understand. but why cant you just make a simple array to have all of them and go through them without having to say String[][] cars = {
    {"Camaro","Corvette","Silverado"},
    {"Mustang","Ranger","F-150"},
    {"Ferrari","Lambo","Tesla"}
    };
    Whats the point of defining them like cars [0][0]? Is it just so you can loop through them and change something in there?
    Like how can you make it so you just define string cars = and then just put the for without going thru each of them 00 01 etc

  • @mensahtribeadventures2630
    @mensahtribeadventures2630 2 роки тому

    how would you bubble sort this 2d array