array 1 « array « Java Collection Q&A

Java Collection Q&A
3.Array Byte
4.Array Char
5.Array Convert
6.Array Dimension
7.Array Integer
8.Array Object
9.Array String
14.Garbage Collection
Java Collection Q&A » array » array 1 

1. How to concatenate two arrays in Java?

I need to concatenate two String arrays in Java.

void f(String[] first, String[] second) {
    String[] both = ???
What is the easiest way to do this?

2. How to disable array bounds checking in opensource Java?

Now that Java is open source, one of the first things I would like to do is to disable array bounds checking for certain blocks of code, where I'm dead sure ...

3. How to display an array to the user

I'm trying to display the contents of an ordered array in something like a JTextField.

for (int i=0; i<array.length; i++) {
This won't work for two reasons. The first ...

4. Storing & lookup double array

I have a fairly expensive array calculation (SpectralResponse) which I like to keep to a minimum. I figured the best way is to store them and bring it back up when ...

5. Java native array lengths

I have a 2D array of doubles in Java which is basically a table of values and I want to find out how many rows it has... It is declared elsewhere (and ...

6. If else alternative

I have a program I am writing that works on the principle of populating a two dimensional array to check the winning condition, it's noughts and crosses so the two dimensional ...

7. Java array problems

I have put few elements in array (e.g. 5 elements) The first element, index[0] of the array will automatically displayed without the user need to click the button. After the button clicked, ...

8. Making a very large Java array

I'm trying to find a counterexample to the Pólya Conjecture which will be somewhere in the 900 millions. I'm using a very efficient algorithm that doesn't even require any ...

9. Java bean machine

Here is what I'm suppose to accomplish:

Write a program that stimulates a bean machine Your program should prompt the user to enter the number of ...

10. How do I find the smallest item in an array?

I have an array, and need to find the index of the smallest item.

a = [2, 2, 2, 3, 3, 4, 3]
This should return 0. Please note that the content and ordering ...

11. Java: How can I feed commandline input into an array?

I'm working on a Bruce Eckel exercise on how to take a keyboard command line input and put it into an array. It's supposed to take 3 separate inputs and place ...

12. Calling a global Array

I'm currently trying to draw shapes with 2D Arrays. In my class there is a global array defined with public char canvas[][]; Up until now, I have only declared arrays with char ...

13. Grabbing a segment of an array in Java

I'm looking for a method in Java that will return a segment of an array. An example would be to get the byte array containing the 4th and 5th bytes of ...

14. Rebuild indices of Java array

I have a bidimensional Object array in Java. Some indices aren´t nor used, because thei were skipped during array fill. Array looks like:

The 2 is missing, how can i rebuild the indices ...

15. array of structures, or structure of arrays?

Hmmm. I have a table which is an array of structures I need to store in Java. The naive don't-worry-about-memory approach says do this:

public class Record {
  final private int ...

16. Why is an array not assignable to Iterable?

with Java5 we can write:

Foo[] foos = ...
for (Foo foo : foos)
or just using an Iterable in the for loop. This is very handy. However you can't write a generic method ...

17. Interpolation over an array (or two)

I'm looking for a java library or some help to write my own interpolation function. That is I have two arrays of doubles which are potentially different sizes, but are ...

18. How to use an array constant in an annotation

I would like to use constants for annotation values.

interface Client {

    @interface SomeAnnotation { String[] values(); }


19. Why can't I create a new Java array inline?

Why does the first one work and the second not work? 1) OK

String[] foo = {"foo"};
2) BAD
Is there a quick way to create a String[] on a single line?

20. Subclassing Array

If you're not interested in a story, skip the first 2 paragraphs. I was talking to a friend about arrays and why they (still) crash if you try to access an object ...

21. Returning array from method

In my class, I have a method that returns an array like this.

double arrValues[] = ClassX.getValues();
I wonder that, does the array size have any influence on performance. Are there any copy ...

22. How to check the end of the array

I have an array of type int matrix[][] and it has values of 0 and 1

the values are different to the above, however this is a random example. What i need to do ...

23. What is the best way to combine several arrays into one single array

Hey, I was trying to combine several arrays of type double into one single array, what is the best way to do it? Thanks!

24. Java Array Initialisation Beginner's Question

A super trivial beginner question on Java arrays: Can anyone explain why the compiler doesn't like this:

class Cycle {}

public class CycleTest {
    Cycle[] cy = new Cycle[3];

25. Pseudo code needed if possible?

I was wondering if I could have some pseudo code for working out the following i need to loop through a 2d array(the method i am working on takes an int). It ...

26. javabat question

Return an array that contains the exact same numbers as the given array, but rearranged so that all the zeros are grouped at the start of the array. The order of ...

27. Determine the most common occurance in an array

Assume I have an array of doubles that looks like the following:

Array[10] = {10, 10, 10, 3, 10, 10, 6, 10, 10, 9, 10}
I need a function that can determine what ...

28. In Java what do arrays inherit from? Can I do this?

Sorry for the newbie question, I'm used to C# so my Java framework knowledge is not so good. I have a couple of arrays:

int[] numbers = new int[10];
String[] names = new String[10];

//populate ...

29. Java time-efficient sparse 1D array (double)

I need an efficient Java structure to manipulate very sparse vectors of doubles: basic read / write operations. I implemented it in a HashMap but the access is too slow. Should ...

30. How should I deal with a very large array in Java?

I have an algorithm which currently allocates a very large array of doubles, which it updates and searches frequently. The size of the array is N^2/2, where N is the ...

31. Display only found results [java]

A follow up to my Item Scanner, a java tool that scans through a directory of *.txt files. Thank you all for the help! However now I have a different problem. When I ...

32. java array assignment question

While using method Arrays.deepToString(Object [] a) I am facing this problem which I can put down in this way.

 Object [] not_allowed = new  int[]{7, 9, 8};
 Object [] allowed ...

33. Array Instantiation

Can you help me out with this question please. Question: Given the following array declarations

double readings[];
String urls[];
TicketMachine[] machines;
write assignments that accomplish the following tasks:
  • make the readings variable refer to an array that ...

34. How to encapsulate an array in Java

i'm starting with Java and i'm learning about setters,getters and encapsulation. I have a very simple program, 2 classes:

  • Container has a private int array (numArray) with his setter & getter.
  • Main creates a Container ...

35. Strange {} Syntax

private final String[] okFileExtensions = new String[] {"csv"};
Would someone please explain why {} is written after a String array decleration? Thanks.

36. Java Arrays - What are they

From looking at the Java Collections API i see that arrays are not regarded as collections. If not what are arrays regarded as?

37. Can Java store methods in arrays?

Well I wrote some code and all I was doing was for loops, but changing which method I called. I tried using a for loop so it'd be a bit neater ...

38. How are arrays implemented in java?

Arrays are implemented as objects in java right? If so, where could I look at the source code for the array class. I am wondering if the length variable in arrays ...

39. logical OR and modulus operator behaving oddly in Java

I'm trying to build an array of prime numbers in Java.

if(c % 2 != 0 || c % 3 != 0 || c % 5 != 0) {

40. Anonymous arrays in BeanShell

Is there a syntax for declaring anonymous arrays in BeanShell? I would like to write code analogous to the following:

print(Arrays.asList("cat", "dog"))
but BeanShell fails to find the "asList" method, presumably because ...

41. how we check for null array in java

I am new bie to java unable to check for null. can you enlighten me on this. I have int array which has no elements I tried this code

int[] k = new int[3];


42. How to store an array returned by a method in Java

I want to store the array returned by a method into another array. How can I do this?

 public int[] method()

int z[] = {1,2,3,5};
return z;
When I call this method, how ...

43. Storing postorder sequence in array

In the code below, the nodes of a postorder tree traversal is always printed I m wondering if there is a way to store these nodes in the postorder sequence in an ...

44. Stop ArrayOutOfBoundsException from halting program execution in Java

Im am currently developing an automated "test" class (running several individual tests on other classes in the same package). The aim of the test file is to show whether each test ...

45. Why might RvdProxy.getServices() incorrectly return an empty Array?

The following code snippet attempts to create a Tib DaemonManager connecting to a particular rvd, and then query for that rvd's services.

public static void main(String[] args) throws RuntimeException {

46. Array inside of Arrays; Java (Help with Lab Assignment)

We are working on a lab assignment for my CS&E class and I think I can ask this question without going into detail of the entire lab requirements, but is it ...

47. Array increment operator

Could any one please tell me the meaning of "++" with array in the following code in Java:

   int [ ] arr = new int[ 4 ];

48. Creating lots of arrays

How do you create, say, 30 arrays (it doesn't matter of what type, say, char[])? It seems to me that it is not a good idea, to create them one by ...

49. How to store arrays in single array

How can I store arrays in single array? e.g. I have four different arrays, I want to store it in single array int storeAllArray [] and when I call e.g. storeAllArray[1] ...

50. creating and returning an array from a method

i currently have a method that checks what is around the centre item in a 3x3 grid, if what is in the 8 adjacent positions is containing what i am checking ...

51. Is there a native Java method to box an array?

Is there a implemented Java method in jdk to do this?

public static Byte[] box(byte[] byteArray) {
Byte[] box = new Byte[byteArray.length];
for (int i = 0; i < box.length; i++) {

52. how to take user input in Array using java?

how to take user input in Array using Java? i.e we are not initializing it by ourself in our program but the user is going to give its value.. please guide!!

53. Java native methods issues with SUN JVM (jdk1.5.0_14) and multi-core CPU’s

We are hosting an application on SUN JVM that handles a lot of XML parsing using Jaxb. The application is parsing the XML fine using JRockit 5 but when using the SUN ...

54. Initializing Multidimentional Arrays

I have a 3-multidimentional array :

int[][][] env;
which i would like to initialize with data in a text file. The data in the text file looks like this:
 {    ...

55. How to mix two arrays in Java?

I have some String[] arrays, for example:

['a1', 'a2']
['b1', 'b2', 'b3', 'b4']
How can I mix them, so that I get ['a1', 'b1', 'c1', 'a2', 'b2', 'b3', 'b4'] (0 element of a, then ...

56. java array manipulation

I'm a beginner in java. I want the logic of the small program. I have two arrays

array = {a1,a2,a3,a4,a5,,,,,,,,,an}
array2 = {b1,b2,b3,b4,,,,,,,,,,,bn}
I want string as a1b1,a2a3b2b3,a4a5a6b4b5b6,.... so on up to n Please tell ...

57. Finding the best person in an array

I have an array called Names[5] and one called scores[5][5]. Each row corresponds with the name in the respective index. I need to find the highest score in the scores array and return ...

58. Java: How do I implement a method that takes 2 arrays and returns 2 arrays?

Okay, here is what I want to do: I want to implement a crossover method for arrays. It is supposed to take 2 arrays of same size and return two new arrays that ...

59. Writing a blackjack console program in Java

I have an assignment of making a blackjack like program in a class. My first problem I am dealing with is creating an array of the cards. The professor wants an ...

60. Arrays of different types

Is it possible to have an array that contains two different types of data? I want to have an array that contains a double and also a string. I attempted:

ArrayList<double><String> array;
But ...

61. Why are public static final array a security hole?

Effective java says:

// Potential security hole! static public final Thing[] VALUES = { ... };
Can somebody tell me what is the security hole?

62. Java - Highest, Lowest and Average

I've just started studying and I need help on one of my exercises. I need the end user to input a rain fall number for each month. I then need to ...

63. Implementing union of Set using basic java array

Note: This is an assignment. Hi, Continuing with my Set implementation using Java basic array, I'm now struggling with the 3 to last function namely the union.


class Set {


64. How do I detect an array

OK so, is there an efficient way to detect on what array you're currently on by using the KeyListener? My code: What I am trying to do here is ...

65. Arrays not counting correctly

I know I was just asking a question earlier facepalm This is in Java coding by the way. Well after everyones VERY VERY helpful advice (thank you guys alot) I managed to get over ...

66. JAVA: storing input into array

I need to write a program where the program would generate random letter and i would need to store this random character into an array


67. In Java why would one want to put the brackets in front of the name of an aray?

I've been told that

int[] numbers 
int numbers[] 
are equivalent. I've only ever seen the former though. What impetus is there, if ever, to write the latter?

68. Basics of working with Arrays in Java

How do you:
1. Initialize (create) an Array.
2. Push a String value into it.
3. Push another String value into it.
4. Dump it to get its contents.

69. How do I add array indices to the XML produced by JAXB?

I have a Java bean ala

@XmlRootElement public class Bean {
    @XmlElementWrapper(name = "ints") @XmlElement(name = "int")
    int[] values;

    // constructors, getters, setters, ...

70. Java: CharBuffer "ignores" array offset?

Please help me understand the following: I create a CharBuffer using CharBuffer.wrapped(new char[12], 2, 10) (array, offset, length)
so I would expect that the array is accessed with an offset of 2 and total ...

71. Java unreachable statement

I'm trying to code a method which loads a map according from a number inside loadBoard(NUMBER_HERE); But I get 'unreachable statment' on the line

return board;
Here is my code:
public int[][] loadBoard(int ...

72. java - change array

OK let's say I have this array:

public int[][] loadBoard(int map) {

    if (map == 1) {  return new int[][] { 
{2,2,24,24,24,24,24,1,3,0,0,0,1 },  {
2,2,24,23,23,23,24,1,3,0,0,0,1 },  {
1,1,24,23,23,23,24,1,3,3,3,3,1 ...

73. Replace in Array

im trying make one replace in string from a array but this dont work

dna[i].replace('T', 'C');
and with this way work?
"ATCTA".replace('T', 'C');
why dont work with array, how i can use use a replace ...

74. How can one know if a property is an array?

I am calling a JavaScript function using the rhino API:

Function fct = context.compileFunction(scope, script, "script", 1, null);
Scriptable result = (Scriptable)

75. Java - How to Give method an array

I have a method like the following:

public void launch(String cmd, String [] args, String workingDir)
Inside this method I call ProcessBuilder. How can I call ProcessBuilder including an arbitrary ...

76. Extending the Arrays-class in Java

I'm trying to add functionality to the Arrays-class. In my project I use the (static) methods from Arrays and have some other methods that also handle array-conversion, sorting, etc... I'm trying to ...

77. divide array into smaller parts

I would like to divide a large byte array into smaller chunks (say 64 bytes). Please help me with this.

78. Java Array concept

 for(int m=0; m< checkBoxValue.length ; m++)
    System.out.println("check box in model class"+checkBoxValue[m]+"\n");

This loop is to print two values in array. It prints the values But after ...

79. Java weird array behavior

Why is it that this works:

int[] array = {1, 2, 3};
but this doesn't:
int[] array;
array = {1, 2, 3};
If I have an array instance variable and I want to initialize it in ...

80. Why does changing an entire Array row create odd behavior?

Simply put. Why did this make my code malfunction after awhile.

//Color[][] colorArr = new Color[Width][Height]();

private void shiftRowsDown(int row) {
    for (int i = row; i > 0; i--)

81. Best way to create singleton array

What is the "easiest" way for you to create a singleton (with exactly one element) Objects array in Java ?

82. Double-checked locking for growable array of binomial coefficients

I'm trying to use double-checked locking to maintain an array of binomial coefficients, but I read recently that double-checked locking doesn't work. Efficiency is extremely important so using volatile isn't ...

83. Returning an Array

I am new to programming and Java and trying to write a program which takes two arrays as input and reports back their sum. I want to do this by creating ...

84. How to use property Array in ant Task?

I have created an Ant task, wherein i would like to have an property array? First of all, is it possible? Does ant allows us to have a property array?

public class ...

85. Immutable array in Java

Is there an immutable alternative to the primitive arrays in Java? Making a primitive array final doesn't actually prevent one from doing something like

final int[] array = new int[] {0, 1, ...

86. Subtypes of Arrays

If I have class A { and class B extends A { will

B[] b = new B[1];
b[0] = new B();
System.out.println(b instanceof A[]);
print out true or false?

87. Java unmodifiable array

final Integer[] arr={1,2,3};
I tried the above code to see whether a final array's variables can be reassigned[ans:it can be].I understand that by a final Integer[] array it means we cannot assign ...

88. Is it ok to return a locally initialised array, if no how can I do it?

Take this example:

public class foo
    public int[] func()
        int arr[] = new int[3];

89. Java array creation

This is just a simple question, and I can't find the answer in the documentation !

String args[] = new String[0];
args[0] = "test";
Is that correct ? Does this creates an array with ...

90. java - Can someone explain to me what is going on? there's a lot of things going on in which I haven't been taught yet

public class Main {

    public static void main(String[] args) {
     int[] x = {1, 2, 3};

91. Beginners Java Array Question

I'm trying to output a square of X's using an array. The diagonals of the square will be filled with 'X' and the empties will be filled with spaces '_'. Here's ...

92. Java Array Question

I'm trying to create a triangle where empty cells have spaces and non empty cells have X's.

public static char[][] Triangle(int size) {

93. strange problem in an array

I'm working on a little server app with Java. So, I'm getting informations from different client, and if information comes in, the following method is called:

public void writeToArray(String data) {

94. array question - what does this line do?

        package javaapplication1;
     public class Main {


95. how to get the most frequent items

I am working on an application which has a large array containing lines of numbers,

transNum[20000][200]//this is the 2d array containing the numbers and always keep track of the line numbers
I am ...

96. What are the weaknesses and shortcomings of arrays?

I would like to know what are the weaknesses of the arrays. I think that it is very helpful to know in order to determine whether the arrays are the best ...

97. Rotate array left

I have a 3x4 array that I want to rotate left once, so that it becomes a 4x3. Imagine a box of values, and just rotate that box left. Here's the ...

98. Array RGB manipulation

Lets say I have an int[][] arrayA and an int[][] arrayB. At any given coordinate in this array lies an RGB value. What I want to do is merge the RGB ...

99. Java Array Manipulation

I have a function named resize, which takes a source array, and resizes to new widths and height. The method I'm using, I think, is inefficient. I heard there's a better ...

100. array of vehicles

is there anyone that can help me with java program I spent hour troubleshooting? I have no problem reading the file into a string array but I don't seem to know ...  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.