Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中的关联数组_Java_Php_Associative Array - Fatal编程技术网

Java中的关联数组

Java中的关联数组,java,php,associative-array,Java,Php,Associative Array,我有PHP背景,我试图创建一个多维数组,但很难理解Java的工作方式。我认为这可以通过使用JSON和GSON库来实现,但在网上学习了一些教程之后,我无法理解这是如何实现的 以下是我在PHP中所追求的,如何在Java中实现同样的目标 function creatCars($id) { $aCars = array( 0 => array( 'name' => 'vauxhall', 'doors

我有PHP背景,我试图创建一个多维数组,但很难理解Java的工作方式。我认为这可以通过使用JSON和GSON库来实现,但在网上学习了一些教程之后,我无法理解这是如何实现的

以下是我在PHP中所追求的,如何在Java中实现同样的目标

function creatCars($id) {

    $aCars = array(
        0 => array(
                'name'  => 'vauxhall',
                'doors' => 5,
                'color' => 'black', 
        ),
        1 => array(
                'name'  => 'peogeot',
                'doors' => 3,
                'color' => 'red', 
        ),
    );

    return $aCars[$id];
}

function printFirstCarName($sName) {
    $aCar = createCars(0);
    echo $aCars['name'];
}

//prints "vauxhall"
printFirstCarName();

Java不是一种松散类型的语言,您必须告诉编译器每个变量将是什么。要在Java中存储这种结构化数据,您应该首先声明一个类并实例化该类的对象。以下是如何实现与PHP代码相同的功能:

class Car {
    private String name, color;
    private int doors;

    Car(String name, int doors, String color) {
        this.name = name;
        this.doors = doors;
        this.color = color;
    }

    public String getName() {
        return this.name;
    }
}

public class CarMainClass {
    public static void main(String[] args) {
        Car[] aCars = new Car[2];

        aCars[0] = new Car("vauxhall", 5, "black");
        aCars[1] = new Car("peogeot", 3, "red");

        System.out.println("First car name is: " + aCars[0].getName());
    }
}
编译时使用:

javac CarMainClass.java
然后运行:

java CarMainClass

您必须先学习Java的基础知识,才能理解上述代码。

您试图实现的似乎是一个“汽车阵列”。因此,我建议不要创建数组,而是直接实现“汽车数组”

为此,我将首先定义汽车,可能在不同的文件中:

class Car {
  //you can make these private and use 'get' and 'set' methods instead
  public String name;
  public String color;
  public int doors;

  public Car() {
    name = "";
    color = "";
    doors = 0;
  }

  public Car(String name, String color, int doors) {
    this.name = name;
    this.color = color;
    this.doors = doors;
  }
}
您可以在另一个模块中使用汽车结构,如下所示:

Car[] cars = new Car[100]; //create one hundred cars
cars[11].doors = 4; //make the 12th car's number of doors to 4 
String[][] twoD = new String[][] {
 {"apple", "banana", "cranberry"},
 {"car", "ship", "bicycle"}
}
HashMap<String, String> map = new HashMap<String, String>();
map.put("car", "drive");
map.put("boat", "swim");

System.out.println("You can " + map.get("car") + " a car.");
System.out.println("And a boat can " + map.get("boat") + ".");
public class Car {

    //member variables
    public String name;
    public int doors;
    public String color;

    //constructor
    public Car(String name, int doors, String color) {
        this.name = name;
        this.doors = doors;
        this.color = color;
    }

}
Car[] cars = new Car[2];
cars[0] = new Car("vauxhall", 5, "black");
cars[1] = new Car("peogeot", 3, "red");

您可以使用更灵活的数据结构,如向量、列表、地图等。。。搜索Java集合,您将找到信息的音调。

您可以创建一个classCar,而不是创建2D数组

   public class Car{
    private String carName;
    private String color;
    private int noOfDoors;

    public car(String carName,int door,String color){
    this.carName=carName;
    this.door=door;
    this.color=color;
    }
    public String getCarName(){
    return getCarName;
    }

    public void setCarName(String carName){
    this.carName=carName;
    }
  // Same getters(getXXX) and setters(setXXX) for other Variables
 }
现在创建上述类的对象

 Car audi=new Car("audi",2,"Black");
 Car bmw=new Car("bmw",4,"White");
现在将这些添加到
列表中


我建议您熟悉HashMaps、Maps和ArrayList。在Java和许多其他语言中,作弊类似于视频游戏

private static Map<Integer, HashMap<String, String> > carMap = new HashMap<Integer, HashMap<String, String> >();
为了更好地实现您想要的,我建议您阅读本文并熟悉一些设计模式。沿着这条路再往前走一点,但是为了休息,看看外面有什么。例如:


当从脚本语言转向强类型语言时,有时您也必须改变您的思维方式。

首先,您应该创建类Car,即:

public class Car {

  enum ColorType {
     BLACK, RED;
  }

  private String name;
  private int doors;
  private ColorType color;

  Car(String name, int doors, ColorType color) {
    this.name = name;
    this.doors = doors;
    this.color = color;
  }

  public String getName() {
    return name;
  }

  public int getDoors() {
    return doors;
  }

  public ColorType getColor() {
    return color;
  }
}
现在您可以使用数组,但更适合您的是使用ArrayList:

List<Car> cars = new ArrayList<Car>();
cars.add(new Car("vauxhall", 5, BLACK));
cars.add(new Car("peogeot", 3, RED));

for (Car car : cars ) {
  System.out.println("Car name is: " + car.getName());
}
List cars=new ArrayList();
添加(新车(“沃克斯豪尔”,5,黑色));
添加(新车(“peogeot”,3,红色));
用于(汽车:汽车){
System.out.println(“车名为:“+Car.getName());
}
PHP中的数组与Java中的数组不同。区别如下:

PHP: PHP数组实际上是字典。它们为每个键存储一个值,其中键可以是整数或字符串。如果您尝试使用其他内容作为键,它将被转换为整数或字符串

爪哇: Java中的数组 Java数组的关联方式与PHP中的不一样。让我们从Java中的一维数组开始:

Java中的一维数组具有固定长度(不能更改),并且每个键都是
0
array.length-1
范围内的整数。所以键,实际上叫做索引,总是整数。另外,在Java中,如果您有一个包含键
2
4
的数组,那么您还(至少)有键
0
1
3
,因为此时长度必须至少为5

Java中的数组也只有一个类型,数组中的每个值只能是指定的类型。数组的大小和类型都不能更改

在Java中创建数组时,有两种可能:

  • 创建数组时显式指定长度

    String[] words = new String[4];
    
    String[] words = new String[] {"apple", "banana", "cranberry"};
    
    变量
    words
    现在保存长度为4的
    String
    类型数组。所有索引(0到3)的值最初设置为
    null

  • 创建数组时指定元素

    String[] words = new String[4];
    
    String[] words = new String[] {"apple", "banana", "cranberry"};
    
    变量
    words
    现在保存长度为3的
    String
    类型数组。所包含的元素与第一个绑定到索引0的元素、第二个绑定到索引1的元素相同,依此类推

  • 您可以将多维数组
    视为包含数组的数组。二维数组可以如下所示:

    Car[] cars = new Car[100]; //create one hundred cars
    cars[11].doors = 4; //make the 12th car's number of doors to 4 
    
    String[][] twoD = new String[][] {
     {"apple", "banana", "cranberry"},
     {"car", "ship", "bicycle"}
    }
    
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("car", "drive");
    map.put("boat", "swim");
    
    System.out.println("You can " + map.get("car") + " a car.");
    System.out.println("And a boat can " + map.get("boat") + ".");
    
    public class Car {
    
        //member variables
        public String name;
        public int doors;
        public String color;
    
        //constructor
        public Car(String name, int doors, String color) {
            this.name = name;
            this.doors = doors;
            this.color = color;
        }
    
    }
    
    Car[] cars = new Car[2];
    cars[0] = new Car("vauxhall", 5, "black");
    cars[1] = new Car("peogeot", 3, "red");
    
    对于这个
    twoD[0][2]
    将是
    “蔓越莓”
    twoD[1][1]
    将是
    “船舶”
    。但是数组的维数并不影响键是整数这一事实

    Java地图: 尽管Java没有用于关联数组的内置语言构造,但它提供了带有各种实现的接口
    Map
    ,例如
    HashMap
    Map
    有一个键的类型和一个值的类型。您可以使用以下地图:

    Car[] cars = new Car[100]; //create one hundred cars
    cars[11].doors = 4; //make the 12th car's number of doors to 4 
    
    String[][] twoD = new String[][] {
     {"apple", "banana", "cranberry"},
     {"car", "ship", "bicycle"}
    }
    
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("car", "drive");
    map.put("boat", "swim");
    
    System.out.println("You can " + map.get("car") + " a car.");
    System.out.println("And a boat can " + map.get("boat") + ".");
    
    public class Car {
    
        //member variables
        public String name;
        public int doors;
        public String color;
    
        //constructor
        public Car(String name, int doors, String color) {
            this.name = name;
            this.doors = doors;
            this.color = color;
        }
    
    }
    
    Car[] cars = new Car[2];
    cars[0] = new Car("vauxhall", 5, "black");
    cars[1] = new Car("peogeot", 3, "red");
    
    答案是: Java中的一对一方式 你的问题的答案是,这是不可能的,因为有些值是字符串,有些是整数。但这是与PHP数组最相似的代码:

    //array of HashMaps which have Strings as key and value types
    HashMap<String, String>[] cars = new HashMap<String, String>[2];
    
    HashMap<String, String> first = new HashMap<String, String>();
    first.put("name", "vauxhall");
    first.put("doors", "5");
    first.put("color", "black");
    
    HashMap<String, String> second = new HashMap<String, String>();
    second.put("name", "peogeot");
    second.put("doors", "3");
    second.put("color", "red");
    
    //put those two maps into the array of maps
    cars[0] = first;
    cars[1] = second;
    
    现在,当您拥有类
    Car
    时,您可以创建一个包含所有汽车的数组,如下所示:

    Car[] cars = new Car[100]; //create one hundred cars
    cars[11].doors = 4; //make the 12th car's number of doors to 4 
    
    String[][] twoD = new String[][] {
     {"apple", "banana", "cranberry"},
     {"car", "ship", "bicycle"}
    }
    
    HashMap<String, String> map = new HashMap<String, String>();
    map.put("car", "drive");
    map.put("boat", "swim");
    
    System.out.println("You can " + map.get("car") + " a car.");
    System.out.println("And a boat can " + map.get("boat") + ".");
    
    public class Car {
    
        //member variables
        public String name;
        public int doors;
        public String color;
    
        //constructor
        public Car(String name, int doors, String color) {
            this.name = name;
            this.doors = doors;
            this.color = color;
        }
    
    }
    
    Car[] cars = new Car[2];
    cars[0] = new Car("vauxhall", 5, "black");
    cars[1] = new Car("peogeot", 3, "red");
    

    这是在Java中实现这一点的更好方法。

    Java是一种强类型语言。可以创建异构数据结构,但这是一个复杂的过程。@Jarrod,您通常在查找重复项方面做得很好;请不要打破这个模式。任何人在所有答案上都投了这么快的反对票,请注意用一个很好的理由来评论!!!就好像有人在痛恨我…不管投票结果如何,你的回答都很好。谢谢分享!:]我希望你得到了你一直在寻找的答案。!这就是这个社区的全部目的。哇。在这篇文章中,仇恨是强烈的。有人刚刚否决了所有答案是真的,这篇文章非常需要版主的关注,我不明白为什么,所有这些答案都非常贴切且信息丰富。如果没有方法且所有字段都是公共的,类就没有多大意义。这段代码实际上会抛出一个NullPointerException,因为所有数组元素最初都是Null。我知道,我只是