简体   繁体   English

将一维数组变成二维数组

[英]making a 1d array into a 2d array

my homework is to make a 1d array into a 2d array but i just cant seem to make it work.我的作业是将 1d 数组变成 2d 数组,但我似乎无法让它工作。

=- these are the error im getting =- 这些是我得到的错误

Car.java:7: error: not a statement
    String[] [] carList = new String[8][2];  {"Honda","Ford","Lamborghini","Subaru","Saturn","Nissan","BMW","Porsche", "Chevy", "VW", "Kia", "Audi", "Ram"}; 
                                              ^

Car.java:7: error: ';' expected
    String[] [] carList = new String[8][2];  {"Honda","Ford","Lamborghini","Subaru","Saturn","Nissan","BMW","Porsche", "Chevy", "VW", "Kia", "Audi", "Ram"}; 
                                                     ^
2 errors

and this is my code这是我的代码

//Car.java

import java.util.Scanner;

public class Car {
    public static void main(String[] args) {
    String[] [] carList = new String[8][2];  {"Honda","Ford","Lamborghini","Subaru","Saturn","Nissan","BMW","Porsche", "Chevy", "VW", "Kia", "Audi", "Ram"}; 
 
// Declaring array literal
       
        Scanner scanner = new Scanner(System.in);
        String carMake = "";
        

        for (int i = 0; i < 3; i++) {

            System.out.print("What car do you want to buy?: ");

            carMake = scanner.nextLine();
            
            while (carMake.isEmpty()){

                System.out.print("Input should not be empty, try again: ");

                carMake = scanner.nextLine();
                // starting loopdeloop 
                
            }
             for (int j = 0; j <= carList.length - 1; j++){
            
            if (carMake.equalsIgnoreCase(carList[j])) {
            System.out.println(j + " " + carMake);
            
           
  // code block to be executed..... for his war crimes
            
            } else {
            System.out.println("404 Car make not found..... look behind you");}
            
          
            
        }
        

            }
            for (int l = 0; l <= carList.length - 1; l++) {
            System.out.print(carList[l] + " ");}
                   
    }
}

I think you need to first study how to initialize a 2d array and then how to traverse through a 2d array.我认为你需要先研究如何初始化一个二维数组,然后再研究如何遍历一个二维数组。

you are not initializing properly here fix this你没有在这里正确初始化修复这个

String[] [] carList = new String[8][2];  {"Honda","Ford","Lamborghini","Subaru","Saturn","Nissan","BMW","Porsche", "Chevy", "VW", "Kia", "Audi", "Ram"};
  1. you are traversing a 2d array using a single loop this is wrong您正在使用单个循环遍历二维数组,这是错误的

     for (int j = 0; j <= carList.length - 1; j++){ if (carMake.equalsIgnoreCase(carList[j])) { System.out.println(j + " " + carMake); }}

here are a few examples if its a 1d array then its a linear list so it will be something like这里有几个例子,如果它是一维数组那么它是一个线性列表所以它会是这样的

String fruits[] = {"apple","banana","orange"}

but when its a 2d array you have to thing it like a table ex:-但是当它是一个二维数组时,你必须把它当作一个表 ex:-

String[][] cars = { {"lamborghini","price 1 million"}, {"skoda","50k"}};

and for iterating if its a 1d array then for loop is enough if its a 2d array then you have to use 2 variables ex:如果它是一个一维数组那么for循环就足够了如果它是一个二维数组那么你必须使用2个变量ex:

    for(int i=0;i<n;i++){
        for(int j=0;j<m;j++){
         print(cars[i][j])
    }
}


     

To show your 2d array you have to set it like this:要显示您的二维数组,您必须这样设置它:

String[] [] carList = {{"Honda","Ford","Lamborghini","Subaru","Saturn","Nissan","BMW","Porsche"},{"Porsche", "Chevy", "VW", "Kia", "Audi", "Ram","Lamborghini","Subaru",}}; 
  

and to loop over this 2d array you have to use two for loops:要遍历这个二维数组,你必须使用两个 for 循环:

for (int i = 0 ; i < 2 ; i++){
    for (int j = 0 ; j < 7 ; j++){
     System.out.println(carList[i][j]);
    }
}

Or dont use a 2d array if you dont need one (which seems like you don't here):或者如果你不需要一个二维数组(这似乎你不在这里),请不要使用它:

String[] carList = {"Honda","Ford","Lamborghini","Subaru","Saturn","Nissan","BMW","Porsche","Porsche", "Chevy", "VW", "Kia", "Audi", "Ram","Lamborghini","Subaru",};

and because this is a 1d array you can loop over it by using one for loop:因为这是一个一维数组,所以你可以使用一个 for 循环遍历它:

for (int i = 0 ; i < carList.length /*16*/ ; i++){
    System.out.println(carList[i]);
}

You have an extra semicolon before the array initializer:在数组初始值设定项之前有一个额外的分号:

    String[] [] carList = new String[8][2];  {"Honda","Ford","Lamborghini","Subaru","Saturn","Nissan","BMW","Porsche", "Chevy", "VW", "Kia", "Audi", "Ram"}; 
                                          ^ here

Remove it.去掉它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM