[완료]java array에 원소추가하기.

opticaller의 이미지

기초적일것같은 질문입니다만, c에서 java로 넘어가려니 쉽지 않네요;

File[] fileArray = file.listFiles();

이렇게 하면 이미 fileArray가 만들어진 상태인데,

이 상태에서 동적으로 원소의 추가가 가능한지요?

또 맨 뒤에 말고 중간에 붙이는 것 (마치 list를 쓰듯)도 가능한가요?

neocoin의 이미지

.

opticaller의 이미지

다른 방법을 찾아봐야겠습니다 ^^

7339989b62a014c4ce6e31b3540bc7b5f06455024f22753f6235c935e8e5의 이미지

ArrayList를 쓰심이...

---8< 서명 -----------------
애니메이션 감상 기록 http://animeta.net/

opticaller의 이미지

사고방식이 이렇게 막혀있으니...
차라리 리스트를 쓰는게 나을 것 같습니다 >.<

public File[] AddToFileArray(File[] aDestArr, File[] aSrcArr)
{
	int _size = aDestArr.length + aSrcArr.length;
	File[] _returns = new File[_size];
 
	for (int iter = 0; iter < aDestArr.length; ++iter)
	{
		_returns[iter] = aDestArr[iter];
	}
	for (int iter = 0; iter < aSrcArr.length; ++iter)
	{
		_returns[aDestArr.length + iter] = aSrcArr[iter];
	}
	return _returns;
}
neocoin의 이미지

적어 주신 방법이 일반적인 방법입니다. 그런데 1.6에 추가된 api로 다음과 같이도 작성할 수 있습니다.

String[] ar1 = "a b c".split(" ");
String[] ar2 = "e f g".split(" ");
 
String[] ar3 = Arrays.copyOf(ar1, ar1.length+ar2.length);
System.arraycopy(ar2, 0, ar3, ar1.length, ar2.length);
 
System.out.println(Arrays.asList(ar3));

위의 코드를 비교하신다면, 이정도 되겠네요.

public <T> T[] mergeArray(T[] ar1, T[] ar2){
	T[] ar3 = Arrays.copyOf(ar1, ar1.length+ar2.length);
	System.arraycopy(ar2, 0, ar3, ar1.length, ar2.length);
	return ar3;
}

Arrays#copyOf 도 내부적으로 System#arraycopy 를 사용하는데 deepcopy가 아니니 동일한 역할의 일반적인 merge 함수가 될 수 있습니다.

ps
오랫만에 eclipse 설치해서 써보네요. 아 좋다... eclipse 만세

opticaller의 이미지

이런 방법도 있군요~

eclipse 설치해놓고 notepad++ 로 작업하는 중입니다. -,-

eclipse는 꼭 프로젝트 단위로 작업해야할 것 같은 압박감을 줍니다;

댓글 달기

Filtered HTML

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

BBCode

  • 텍스트에 BBCode 태그를 사용할 수 있습니다. URL은 자동으로 링크 됩니다.
  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param>
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.

Textile

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • You can use Textile markup to format text.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Markdown

  • 다음 태그를 이용하여 소스 코드 구문 강조를 할 수 있습니다: <code>, <blockcode>, <apache>, <applescript>, <autoconf>, <awk>, <bash>, <c>, <cpp>, <css>, <diff>, <drupal5>, <drupal6>, <gdb>, <html>, <html5>, <java>, <javascript>, <ldif>, <lua>, <make>, <mysql>, <perl>, <perl6>, <php>, <pgsql>, <proftpd>, <python>, <reg>, <spec>, <ruby>. 지원하는 태그 형식: <foo>, [foo].
  • Quick Tips:
    • Two or more spaces at a line's end = Line break
    • Double returns = Paragraph
    • *Single asterisks* or _single underscores_ = Emphasis
    • **Double** or __double__ = Strong
    • This is [a link](http://the.link.example.com "The optional title text")
    For complete details on the Markdown syntax, see the Markdown documentation and Markdown Extra documentation for tables, footnotes, and more.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 사용할 수 있는 HTML 태그: <p><div><span><br><a><em><strong><del><ins><b><i><u><s><pre><code><cite><blockquote><ul><ol><li><dl><dt><dd><table><tr><td><th><thead><tbody><h1><h2><h3><h4><h5><h6><img><embed><object><param><hr>

Plain text

  • HTML 태그를 사용할 수 없습니다.
  • web 주소와/이메일 주소를 클릭할 수 있는 링크로 자동으로 바꿉니다.
  • 줄과 단락은 자동으로 분리됩니다.
댓글 첨부 파일
이 댓글에 이미지나 파일을 업로드 합니다.
파일 크기는 8 MB보다 작아야 합니다.
허용할 파일 형식: txt pdf doc xls gif jpg jpeg mp3 png rar zip.
CAPTCHA
이것은 자동으로 스팸을 올리는 것을 막기 위해서 제공됩니다.