I put Char [] in the list in Java, but I want to save it in Char [] when I take it out again, what should I do?

Asked 2 years ago, Updated 2 years ago, 35 views

temp =new char[10];     

List  Temp=new ArrayList();         

Temp.add(temp);           

//Another object (Listemp)

char[ ] Temp;


Temp =(char[ ]) temp.get(0);

Error Occurred java.lang.IndexOutOfBoundsException

So we're going to take the char[] form of the list I want to move to a char[] array of other objects, what should I do?

java list

2022-09-21 15:58

2 Answers

If the current example is in the same block, even the IndexOutOfBoundsException error cannot be checked.

It's easy to answer if you give me an example correctly.

I think you're trying to do the following, as I understand. I don't know if that's right

char[] tempArray = new char[10];
System.out.println(tempArray.hashCode());

List tempList = new ArrayList();
tempList.add(tempArray);

char[] targetArray = (char[]) tempList.get(0);
System.out.println(targetArray.hashCode());


2022-09-21 15:58

To summarize the questions,

It is understood that you want to copy the char[] array in the list to another array.

char[] temp new char[10];

List<char[]> list = new ArrayList<>();

list.add(temp);


// To copy to a new array after this...

char[] copyTarget;

char[] src= list.get(0);

// Create the same size of the new array as the existing array, as follows:
copyTarget = new char[src.length];

// Copy through System.arraycopy.
System.arraycopy(src, 0, copyTarget, 0, copyTarget.length);


2022-09-21 15:58

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.