Arrays.asList for primitives

Ok, this may sound like a stupid question, but I've checked the java API and searched this forum and cant find an answer, so here goes:
Is there any equivilent for Arrays.asList for arrays of primitives? I need to get an array of ints in to a list.
Is my hangover just causing me to miss something really stupid?

In case anyone in Sun is listening...
Please provide a class/method for creating human-readable Strings from the following...
int[]
byte[]
float[]
and any other primitive arrays.

Similar Messages

  • Create array of arbitrary dimensions / get Class object for primitive types

    Here's an interesting problem: is there any way to code for the creation of an n-dimensional array where n is only known at runtime (input by user, etc.)?
    java.lang.reflect.Array has a newInstance method which returns an array of an arbitrary number of dimensions specified by the int[] argument, which would give me what I want. However, the other argument is a Class object representing the component type of the array. If you want to use this method to get an array of a primitive type, is it possible to get a Class object representing a primitive type?

    That doesn't help, since the whole problem is that the number of dimensions aren't known until runtime. You can't simply declare a variable of type int[][] or use an int[][] cast, since the number of []'s is variable.
    The int.class construction was partly what I was looking for. This returns the array needed (assuming some read method):
    int dimensions = read();
    int size = read();
    int[] index = new int[dimensions];
    for (int i = 0; i < dimensions; i++)
        index[i] = size;
    Object myArray = Array.newInstance(int.class, index);The only problem is, as an Object, I can't use myArray usefully as an array. And I can't use a cast, since that requires a fixed number of dimensions.

  • Arrays of arbitrary dimensions / Class object for primitive types

    Here's an interesting problem: is there any way to code for the creation of an n-dimensional array where n is only known at runtime (input by user, etc.)?
    java.lang.reflect.Array has a newInstance method which returns an array of an arbitrary number of dimensions specified by the int[] argument, which would give me what I want. However, the other argument is a Class object representing the component type of the array. If you want to use this method to get an array of a primitive type, is it possible to get a Class object representing a primitive type?

    Devil is in the detailspublic class Test2 {
      public static void main(String[] args) {
        Object[] myArray = new Object[10];
        fillDimensionalArray(myArray, 5, 5);
      public static void fillDimensionalArray(Object[] array, int dim, int size) {
        if (dim==1) {
          for (int i=0; i<size; i++) {
            array[i] = new int[size];
            for (int j=0; j<size; j++) ((int[])array)[j]=999;
    } else {
    for (int i=0; i<array.length; i++) {
    array[i] = new Object[size];
    for (int j=0; j<size; j++) {
    fillDimensionalArray( (Object[]) array[i], dim - 1, size);

  • Achieveing 'by reference' in java for primitives

    Hi,
    I know java passes primitives as 'value' but if someone wishes to pass them by reference..is it doable? some trick to achieve this?
    example of what I'm trying to achieve.
    Collection roles = null;
    boolean d = blah.getPassOrFail(roles);
    roles.size();
    getPassOrFail(Collection roles1)
    roles = getRolesForUser("user2");
    roles.size();
    if (roles.size()>0)
        return true;
    else
        return false;
    }Thanks

    Thanks everyone for the input.
    @ duffy it was really a pseudo i did not intend on making a complete program to illustrate my problem. Now that I take a closer look at it, you are right it would return a nullpointerexception
    I'm aware that this isnt a good coding practice and yes there might be a design flaw But I was trying to achieve this 'a quick and dirty way'.
    I am working on someone else's code.
    The whole back story is that there is a method (getPassOrFail in this instance) which is being called before my code addition.
    getPassOrFail calls a method (lets call it Foo) inside it which gathers bunch of data and puts it in a collection and depending on the size of the returned Collection getPassOrFail returns different 'strings'.
    Now my code addition, which has to come after call to getPassOrFail, needs the data that was put in a Collection inside getPassOrFail (but was never returned).
    So I was thinking if there was a way I could get that Collection, from inside getPassOrFail, rather than me calling Foo to fill the collection. So I was just trying to stop calling a method twice.
    A more simpler approach would be for my code to just directly call Foo for the Collection.
    So is it better if I do this (following duffy's code)
    import java.util.Arrays;
    import java.util.Collection;
    import java.util.HashMap;
    import java.util.Map;
    * PassByValue
    * User: Michael
    * Date: Nov 23, 2007
    * Time: 2:11:43 PM
    public class PassByValue
       private static final Map ROLES_FOR_USER;
       static
          ROLES_FOR_USER = new HashMap();
          ROLES_FOR_USER.put("user1", Arrays.asList("butcher"));
          ROLES_FOR_USER.put("user2", Arrays.asList("baker"));
          ROLES_FOR_USER.put("user3", Arrays.asList("candlestick maker"));
       public static void main(String[] args)
          PassByValue pbv = new PassByValue();
          Collection roles = null;
          String user = "user2";
          boolean pass = pbv.getPassOrFail(user, roles);
          System.out.println((pass ? "pass" : "fail"));
          System.out.println("roles: " + roles);
          roles = pbv.getRolesForUser(user); //getRolesForUser is being called twice...
          System.out.println("roles: " + roles);
       public boolean getPassOrFail(String user, Collection roles)
          roles = getRolesForUser(user);
          return ((roles.size() > 0) ? true : false);
       private Collection getRolesForUser(String user)
          return (Collection) ROLES_FOR_USER.get(user);
    }Edited by: bhaarat_java on Nov 23, 2007 8:36 PM
    Edited by: bhaarat_java on Nov 23, 2007 8:37 PM

  • Using Arrays.asList: Is Array[x] == List.get(x)

    I have an Object[] Array and I wish to remove index 'x' from that Object[] Array. If I use Arrays.asList() passing in the existing Object[] Array I will get new List Object containing the objects from the Object[] Array. Is it guaranteed that index 'x' from the Object[] Array is the same Object from the index 'x' in the new List?
    Object[] obj = new String[] {"obj_0", "obj_1", "obj_2", "obj_3"};
    List list = Arrays.asList(obj);
    assertTrue(obj[0] == list.get(0));
    assertTrue(obj[1] == list.get(1));
    assertTrue(obj[2] == list.get(2));
    assertTrue(obj[3] == list.get(3));Are the above assertions always guaranteed to be true?

    Jared_java_dev wrote:
    I thought the JavaDoc API would state it as well. I did originally check that.
    I agree that it should keep the order but I wanted to verify this behavior. It would make some coding logic much more simple.
    Objective is to remove ojb[2]
    String[] obj = new String[] {"obj_0", "obj_1", "obj_2", "obj_
    //Perferred logic
    List<String> list = Arrays.asList(obj);
    //remove unwanted element
    list.remove(2);
    //create new Object[] without unwanted element
    obj = (String[]) list.toArray();
    {code}
    as opposed to
    {code}
    String[] obj = new String[] {"obj_0", "obj_1", "obj_2", "obj_
    //unwanted logic
    List<String> list = Arrays.asList(obj);
    for (int x = 0; x < obj.length; x++) {
    // == or String.equals
    if (list.get(x) == (obj[x])) {
            list.remove(x);
    obj = (String[]) list.toArray();I guess that I could write a sample program and run to verify this .
    Thanks for everyone's input.No, you won't be able to simply 'delete' items from this array-backed list. It is a fixed size list, so additions/deletions are expected to throw exceptions.
    So now my question is: Why is the master an array in the first place? Seems like it should be a List from the get-go.

  • Error  to the past in the Eclipse - List String tags = Arrays.asList("Hello", "Tutorial"); in

    Hi,
    When I past the Setting properties List<String> tags = Arrays.asList("Hello", "Tutorial"); in the Eclipse I have this error:
    The type List is not generic; it cannot be parameterized with arguments <String>            HelloWorldServlet.java      /hello-world/src/com/sap/cloud/sample/helloworld      line 98           Java Problem
    Why?
    Weides

    Hi Weides...
    Maybe, accidently, when you first hit "organize imports" after you pasted that code fragment, and you were asked which "List" you want to import most probably you selected java.awt.List (In java you have at least the awt list and the java.util.List)
    Now... do the following:
    1. Delete the import statement of import java.awt.List and delete the import java.util.List
    2. Put back List<String>
    3. Then, Organize Imports again , and when it prompted for the choice, choose java.util.List.
    I hope that works for you.
    Regards.
    (PD. Useful checks will be appreciated)

  • Is there arrays or FOR loops in ABAP?

    Hi everyone!
    Is there an array and FOR loops in ABAP similar to the C Language?
    If there is, please give example. _
    Thanks a lot!

    Hi,
    There is no array concept in ABAP but there are field-symbols which are same as pointers in C.As for the FOR loop there is no such command in SAP/ABAP but we do have the do..while and loop...endloop commands.There is also CASE statements and if..endif commands.
    Thanks,
    Sandeep.

  • Associative array type for each blob column in the table

    i am using the code in given link
    http://www.oracle.com/technology/oramag/oracle/07-jan/o17odp.html
    i chnages that code like this
    CREATE TABLE JOBS
    JOB_ID VARCHAR2(10 BYTE),
    JOB_TITLE VARCHAR2(35 BYTE),
    MIN_SALARY NUMBER(6),
    MAX_SALARY NUMBER(6),
    JOBPIC BLOB
    CREATE OR REPLACE PACKAGE associative_array
    AS
    -- define an associative array type for each column in the jobs table
    TYPE t_job_id IS TABLE OF jobs.job_id%TYPE
    INDEX BY PLS_INTEGER;
    TYPE t_job_title IS TABLE OF jobs.job_title%TYPE
    INDEX BY PLS_INTEGER;
    TYPE t_min_salary IS TABLE OF jobs.min_salary%TYPE
    INDEX BY PLS_INTEGER;
    TYPE t_max_salary IS TABLE OF jobs.max_salary%TYPE
    INDEX BY PLS_INTEGER;
    TYPE t_jobpic IS TABLE OF jobs.jobpic%TYPE
    INDEX BY PLS_INTEGER;
    -- define the procedure that will perform the array insert
    PROCEDURE array_insert (
    p_job_id IN t_job_id,
    p_job_title IN t_job_title,
    p_min_salary IN t_min_salary,
    p_max_salary IN t_max_salary,
    p_jobpic IN t_jobpic
    END associative_array;
    CREATE OR REPLACE package body SHC_OLD.associative_array as
    -- implement the procedure that will perform the array insert
    procedure array_insert (p_job_id in t_job_id,
    p_job_title in t_job_title,
    p_min_salary in t_min_salary,
    p_max_salary in t_max_salary,
    P_JOBPIC IN T_JOBPIC
    ) is
    begin
    forall i in p_job_id.first..p_job_id.last
    insert into jobs (job_id,
    job_title,
    min_salary,
    max_salary,
    JOBPIC
    values (p_job_id(i),
    p_job_title(i),
    p_min_salary(i),
    p_max_salary(i),
    P_JOBPIC(i)
    end array_insert;
    end associative_array;
    this procedure is called from .net. from .net sending blob is posiible or not.if yes how

    Ok, that won't work...you need to generate an image tag and provide the contents of the blob column as the src for the image tag.
    If you look at my blog entry -
    http://jes.blogs.shellprompt.net/2007/05/18/apex-delivering-pages-in-3-seconds-or-less/
    and download that Whitepaper that I talk about you will find an example of how to do what you want to do. Note the majority of that whitepaper is discussing other (quite advanced) topics, but there is a small part of it that shows how to display an image stored as a blob in a table.

  • How can I use two single-dimensional arrays-one for the titles and array

    I want to Use two single-dimensional arrays-one for the titles and one for the ID
    Could everyone help me how can i write the code for it?
    Flower
    public class Video
    public static void main(String[] args) throws Exception
    int[][] ID =
    { {145,147,148},
    {146,149, 150} };
    String[][] Titles=
    { {"Barney","True Grit","The night before Christmas"},
    {"Lalla", "Jacke Chan", "Metal"} };
    int x, y;
    int r, c;
    System.out.println("List before Sort");
    for(c =0; c< 3; ++c)
    for(r=0; r< 3; ++ r)
    System.out.println("ID:" + ID[c][r]+ "\tTitle: " + Titles[c][r]);
    System.out.println("\nAfter Sort:");
    for(c =0; c< 3; ++c)
    for(r=0; r< 3; ++ r)
    System.out.println("ID:" + ID[c][r]+ "\tTitle: " + Titles[c][r]);

    This is one of the most bizarre questions I have seen here:
    public class Video
    public static void main(String[] args) throws Exception
    int[] ID = {145,147,148, 146,149, 150};
    String[] Titles= {"Barney","True Grit","The night before Christmas", "Lalla", "Jacke Chan", "Metal"};
    System.out.println("List before Sort");
    for(int i = 0; i < Titles.length; i++)
       System.out.println("ID:" + ID[i]+ "\tTitle: " + Titles);
    System.out.println("\nAfter Sort:");
    for(int i = 0; c < Titles.length; i++)
    System.out.println("ID:" + ID[i]+ "\tTitle: " + Titles[i]);
    Generally you don't use prefix (++c) operators in you for loop. Use postfix (c++).
    Prefix means that it will increment the variable before the loop body is executed. Postfix will cause it to increment after.

  • TreeSet vs Collection.Sort / Array.sort for Strings

    Gurus
    I am pondering weather to use TreeSet vs the Collections.sort / Array.sort for sorting Strings.
    Basically I have a list of Strings, i need to perform the following operations on these Strings
    1) Able to list Strings starting with a Prefix
    2) Able to list Strings Lexically greater than a String
    Any help would be greatly appreciated!
    Thanks a bunch

    SpaceShuttle wrote:
    Gurus
    I am pondering weather to use TreeSet vs the Collections.sort / Array.sort for sorting Strings.
    Basically I have a list of Strings, i need to perform the following operations on these Strings
    1) Able to list Strings starting with a Prefix
    2) Able to list Strings Lexically greater than a String
    Any help would be greatly appreciated!
    Thanks a bunchBig-O wise, there's no difference between sorting a list of N elements or inserting them one by one in a tree-set. Both take O(n*log(n)). But both collections are not well suited for looking up string that start with a certain substring. In that case you had better use a Patricia tree (or Radix tree).
    Good luck.

  • Build 1D array using for loop

    Hi guys,
    I am testing out array. How do I create a 1D array of x-y using for loop?

    Nextal wrote:
    I am testing out array. How do I create a 1D array of x-y using for loop?
    OK, first of all you did not say what the array should contain. If you want an array that has one array element for each iteration of the while loop, you need to first think a bit harder. Your outer loop has no wait, thus it will spin millions of times per second and you will run out of memory soon, no matter how much memory you have. How many times should the while loop iterate? If you know the number before the program starts, you would use a FOR loop and autoindex at the loop boundary.
    If you want to use a whiile loop, you need a mechanism to keep the array size reasonable. For small final arrays, you can just build the array in a shift register if you need it updated as it happens. If you only need the array after the loop stops, you can autoindex at the right loop boundary.
    In any case, you should take a step back and decide what you really want. Currently the specifications are very vague and unreasonable.
    LabVIEW Champion . Do more with less code and in less time .

  • Collection framework for primitive types

    Hi,
    I am developing an open source collection library for primitive data types. It has just moved out of beta and into production development, menaing that from now on, the API is backwards compatible.
    The sources, binaries, and docs can be downloaded from: http://pcj.sourceforge.net.
    Comments and suggestions are always welcome!
    Regards,
    S&oslash;ren Bak

    Thanks, I'm downloading it now.

  • SDDM 3.1 PROD: Changing parent diagram for primitive processes

    It seems that it is only possible to change the parent diagram of Composite processes.
    Why is that? I cannot se the reason why it should not be possible to change the parent diagram of a primitive process...
    Regards,
    Marc de Oliveira

    I use the object id when synchronizing the reporting schema export with my APEX application.
    When the object ID is deleted and a new is created, I loose the related information about the primitive process stored in APEX. As an example I create an APEX Feature for each Data Modeler process where APEX developers can track the development progress of every process.
    >
    We don't support right now "move" operation for versioned objects.
    The strange thing about this is that Data Modeler does allow the "move" operation of composite processes but not for primitive processes. Why the difference? Why not allow the "move" operation of both types of processes?
    Regards,
    Marc de Oliveira

  • Container for primitive datatypes?

    Which is the best way to store primitive datatypes if I need the functionality to expand and shrink the container.
    I've tried to use Vector and Arraylist but I have to convert each datatype to it's wrapper and add them one by one.
    Aren't there some container which have a method like. Container.add(primitive[] myprimitive)?

    Sort of related: JDK 1.4 has some new IO packages that have primitive buffers for primitive types (java.nio). Not exactly what you're looking for, but just thought I'd throw in my two cents.
    Best bet is to write your own container that acts like a Vector, but only provide add and get methods which accept and return doubles/ints/etc.
    class DoubleContainer
      java.util.Vector vec = new java.util.Vector();
      public DoubleContainer()
      void add(double d)
        vec.add(new Double(d));
      double get(int index)
        return ( (Double)vec.get(index)).doubleValue();
      // some more methods might be needed here, like insert, remove...
    }

  • Max array dimension for Speedy 33?

    Hi all,
    Does any body know what the max array dimension for Speedy 33? I am able to create 1-row arrays, but not arrays more than 1 rows..
    Thanks so much in advance!
    ~Cassiopea

    There is a really good knowledge base article on our website that discusses in detail how arrays are handled using LabVIEW DSP and how you should use them. It has exactly what you're looking for.
    How Can I Be Successful With Arrays In the LabVIEW DSP Module?
    Good luck with your application!
    Nick R
    NI

Maybe you are looking for

  • The problem is this: In the phase "Create Java Users"

    Hello, I am installing Solution Manager 4.0 Release 2 (with Red Hat Enterprise Linux Server release 5.1 and Oracle 10 and my java version is: j2sdk1.4.2_14) The problem is this: In the phase "Create Java Users" I get the next error WARNING 2009-02-13

  • Clean up my mac.

    Can someone tell me how to clean my mac of unwanted downloads and apps which are not wanted anymore , but are slowing down my system please.          Have recently set up Parallels 9  , but cant believe that would slow my system down as much as it ha

  • Incorrect domain name, switching to 'untrusted' :newbies here..

    Starting emulator in execution mode Running with storage root DefaultColorPhone Incorrect domain name, switching to 'untrusted' java.lang.SecurityException: untrusted domain is not configured at com.sun.midp.security.Permissions.forDomain(+98) at com

  • Delete Master Data

    Hi all, is the normal rsa1 functionality the only possibility to delete a master data record? I've the problem of time out because I'm only able to start the process as dialoug. Thanks for help Andy

  • I can't turn on iCloud backup on one of my phones after updating to ios7 - help!

    Updated both iPhone 4S to iOS 7, I can get the iCloud backup for one phone, but I don't have the option to turn it on on my other phone. I have reset, uninstalled, reinstalled, signed in/out, I can't figure out what to do. I've had no other problems,