开发者

Add contents to the end of a float array like this

Hey Guys, I've got the following float array...

         public static float camObjCoord[] = new float[] {



             -2.0f, -1.5f,  -6.0f,
              2.0f, -1.5f,  -6.0f,
             -2.0f,  1.5f,  -6.0f,
              2.0f,  1.5f,  -6.0f,

             -2.0f, -1.5f, -10.0f,
             -2.0f,  1.5f, -10.0f,
              2.0f, -1.5f, -10.0f,
              2.0f,  1.5f, -10.0f,

             -2.0f, -1.5f,  -6.0f,
             -2.0f,  1.5f,  -6.0f,
             -2.0f, -1.5f, -10.0f,
             -2.0f,  1.5f, -10.0f,

              2.0f, -1.5f, -10.0f,
              2.0f,  1.5f, -10.0f,
              2.0f, -1.5f,  -6.0f,
              2.0f,  1.5f,  -6.0f,

             -2.0f,  1.5f,  -6.0f,
              2.0f,  1.5f, -6.0f,
             -2.0f,  1.5f, -6.0f,
              2.0f,  1.5f, -10.0f,

             -2.0f, -1.5f,  -6.0f,
             -2.0f, -1.5f, -10.0f,
              2.0f, -1.5f,  -6.0f,
              2.0f, -1.5f, -10.0f,



                 -2.0f,  2.5f,  -6.0f,
                  2.0f,  2.5f,  -6.0f,
                 -2.0f,  4.5f,  -6.0f,
                  2.0f,  4.5f,  -6.0f,

                 -2.0f,  2.5f, -10.0f,
                 -2.0f,  4.5f, -10.0f,
                  2.0f,  2.5f, -10.0f,
                  2.0f,  4.5f, -10.0f,

              开发者_开发知识库   -2.0f,  2.5f,  -6.0f,
                 -2.0f,  4.5f,  -6.0f,
                 -2.0f,  2.5f, -10.0f,
                 -2.0f,  4.5f, -10.0f,
                                      2.0f,  2.5f, -10.0f,
                  2.0f,  4.5f, -10.0f,
                  2.0f,  2.5f,  -6.0f,
                  2.0f,  4.5f,  -6.0f,

                 -2.0f,  4.5f,  -6.0f,
                  2.0f,  4.5f, -6.0f,
                 -2.0f,  4.5f, -6.0f,
                  2.0f,  4.5f, -10.0f,

                 -2.0f,  2.5f,  -6.0f,
                 -2.0f,  2.5f, -10.0f,
                  2.0f,  2.5f,  -6.0f,
                  2.0f,  2.5f, -10.0f,
        };

I've got a method after it which I would like to add values to the end of the array but it's telling me it can't find camObjCoord, any idea why?


Some important points:

  • Arrays in Java are fixed-length objects. You can't modify the length of an array once it's created.
    • You can create another array of size N+1, copying all N elements, and then adding the extra element, but this is a costly highly-inefficient O(N) operation to add a single element
  • Based on the name, formatting and the pattern in the numbers, it looks like every 3 float is a 3D point. You should seriously considering defining a Point3D custom type to hold your data.
  • Effective Java 2nd Edition, Prefer lists to arrays; you should consider having a List<Point3d>
  • Unless you have a good reason to use float, you should use double instead


Adding to an array is not possible because an array can't change in size. So you either use a Collection structure or you create a bigger array, copy the old values to the new one and fill the empty space at the end wih your new values.

But that was not the question - you'll be able access the array

  • with camObjCoord from the same class
  • with MyClass.camObjCoord from a class within the same package (assuming MyClass is the class that contains camObjCoord and
  • with MyClass.camObjCoord from a class within of a different package if this class imports MyClass


It's probably not telling you it can't find the variable, but rather that you're not allowed to add items to an array. Arrays are fixed-length; if you want an extensible array-like data structure, use ArrayList. Alternatively, define your array initially with the size you need it to have.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜